Skip to content

Instantly share code, notes, and snippets.

@deepakg
Created April 21, 2018 09:22
Show Gist options
  • Save deepakg/1da73fcf3313764e90cb5765336e53d4 to your computer and use it in GitHub Desktop.
Save deepakg/1da73fcf3313764e90cb5765336e53d4 to your computer and use it in GitHub Desktop.
A Python program to create a tiled collage from a single image. For more details see: https://www.deepakg.com/the-tiled-fa%C3%A7ades-of-porto
import argparse
from pprint import pprint as pp
from PIL import Image
def make_row_tile(tile, bgcolor, padding, flip_vertical):
(tile_width, tile_height) = tile.size
row_tile = Image.new('RGB',
(tile_width * 2 + padding, tile_height),
color=bgcolor)
row_tile.paste(tile, (0 , 0))
if flip_vertical:
row_tile.paste(tile.transpose(Image.FLIP_LEFT_RIGHT),
(tile_width + padding, 0))
else:
row_tile.paste(tile, (tile_width + padding, 0))
# row_tile.show()
return row_tile
def make_quad_tile(row_tile, bgcolor, padding, flip_horizontal):
quad_tile = Image.new('RGB',
(row_tile.width, row_tile.height * 2 + padding),
color=bgcolor)
quad_tile.paste(row_tile, (0,0))
if flip_horizontal:
quad_tile.paste(row_tile.transpose(Image.FLIP_TOP_BOTTOM),
(0, row_tile.height + padding))
else:
quad_tile.paste(row_tile, (0, row_tile.height + padding))
# quad_tile.show()
return quad_tile
def make_background(path, bgcolor='#fff', padding=10, flip_vertical=False,
flip_horizontal=False):
rows,cols = 10,10
tile = Image.open(path)
(tile_width, tile_height) = tile.size
row_tile = make_row_tile(tile, bgcolor, padding, flip_vertical)
quad_tile = make_quad_tile(row_tile, bgcolor, padding, flip_horizontal)
# print(quad_tile.width, quad_tile.height)
canvas = Image.new('RGB', ((tile_width + padding) * cols - padding,
(tile_height + padding) * rows - padding),
color=bgcolor)
for y in range(0,5):
for x in range(0,5):
# print(str(quad_tile.width*x+10), ",", str(quad_tile.height*y+10))
# print(str((quad_tile.width+10)*x), ",", str((quad_tile.height+10)*y))
canvas.paste(quad_tile,((quad_tile.width + padding) * x,
(quad_tile.height + padding) * y))
return canvas
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Make tiled backgrounds')
parser.add_argument('tile', help='Image file to use as tile')
parser.add_argument('--output', help='Image file to save to. By default the program will just open the montage in the default image viewing program on your OS (e.g. Preview on macOS) and exit.')
parser.add_argument('--bgcolor', default='#ffffff',
help='Background color (valid hex value e.g. #fffff for white)')
parser.add_argument('--padding', default=10, type=int,
help="Gap between each tile")
parser.add_argument('--mirror_col', action='store_true',
default=False,
help='Mirror the first tile vertically')
parser.add_argument('--mirror_row', action='store_true',
default=False,
help='Mirror the first row of tiles horizontally')
args = parser.parse_args()
# pp(args)
output = make_background(args.tile, bgcolor=args.bgcolor,
padding=args.padding,
flip_vertical=args.mirror_col,
flip_horizontal=args.mirror_row)
if args.output:
output.save(args.output)
else:
output.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment