#!/usr/bin/python # -*- coding: utf-8 -*- """Interleave images for opacity holograms. Feed me several images of the same size, and I will output an image with their pixels interleaved such that you could use a shiftable mask on top of them to pick out one of the images. """ from __future__ import division import sys import Image # python-imaging aka PIL def main(argv): image_mode = "RGB" image_names = argv[1:] images = [Image.open(name).convert(image_mode) for name in image_names] image_sizes = [image.size for image in images] image_size = image_sizes[0] assert all(size == image_size for size in image_sizes), image_sizes shape = tuple(reversed(squarish_factorization_of(len(images)))) sys.stderr.write((u"%s×%s images\n" % shape).encode("utf-8")) combined_shape = (shape[0] * image_size[0], shape[1] * image_size[1]) output_image = Image.new(image_mode, combined_shape) for yy in range(combined_shape[1]): ya, yb = divmod(yy, shape[1]) for xx in range(combined_shape[0]): xa, xb = divmod(xx, shape[0]) image_index = xb + shape[0] * yb pixel = images[image_index].getpixel((xa, ya)) output_image.putpixel((xx, yy), pixel) output_image.save("interleave-output.png") def squarish_factorization_of(number): assert number > 0 for p in range(1, number+1): div, mod = divmod(number, p) if p > div: break if mod == 0: best = p return [best, number // best] assert squarish_factorization_of(12) == [3, 4] assert squarish_factorization_of(15) == [3, 5] assert squarish_factorization_of(16) == [4, 4] assert squarish_factorization_of(20) == [4, 5] assert squarish_factorization_of(49) == [7, 7] if __name__ == '__main__': main(sys.argv)