Created
March 9, 2011 09:42
-
-
Save falsetru/861951 to your computer and use it in GitHub Desktop.
concat images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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