Skip to content

Instantly share code, notes, and snippets.

@falsetru
Created March 9, 2011 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save falsetru/861951 to your computer and use it in GitHub Desktop.
Save falsetru/861951 to your computer and use it in GitHub Desktop.
concat images
import Image
import sys
def concat(images, wfn, hfn, xm, ym):
width = wfn(im.size[0] for im in images)
height = hfn(im.size[1] for im in images)
result = Image.new(images[0].mode, (width, height))
x = y = 0
for im in images:
print im.size
result.paste(im, (x, y))
x += im.size[0] * xm
y += im.size[1] * ym
return result
def concat_horizontal(images): return concat(images, sum, max, 1, 0)
def concat_vertical(images): return concat(images, max, sum, 0, 1)
if __name__ == '__main__':
mode = sys.argv[1]
input_filenames = sys.argv[2:-1]
result_filename = sys.argv[-1]
input_images = [Image.open(fn) for fn in input_filenames]
if mode == '-h':
result = concat_horizontal(input_images)
elif mode == '-v':
result = concat_vertical(input_images)
else:
assert False, 'Mode should be given (-h or -v): %s <mode> <input1> <input2> <input3> ... <output>' % sys.argv[0]
result.save(result_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment