#!/usr/bin/python "recipes mostly from http://www.pythonware.com/library/pil/handbook/image.htm" import sys, os, random from PIL import Image DEBUGGING = True def bw(sourcefile, savefile): "convert to black-and-white rendition of same image" image = Image.open(sourcefile) bw = image.convert('L') bw.save(savefile, image.format) return bw def crosseyed(sourcefile, savefile): "convert black-and-white image to double image that must be viewed crosseyed" image = Image.open(sourcefile) if image.mode != 'L': debug('converting mode %s to L (black and white)' % image.mode) image = bw(sourcefile, '/tmp/bw.tmp') image.thumbnail((160, 320)) # no more than 160 pixels wide width, height = image.size crossed = Image.new('L', (width << 1, height), None) sourcedata = list(image.getdata()) white = max(sourcedata) destdata = [] for i in range(height): index = width * i split = randsplit(sourcedata[index:index + width], white) if i < 5 or i > height - 5: for j in range(4): # black boxes help eye align images into one split[0][j] = 0 split[1][j] = 0 destdata += split[0] + split[1] crossed.putdata(destdata) crossed.save(savefile, image.format) def randsplit(pixels, white): pixmap = map(None, range(len(pixels)), pixels) #debug(pixmap) remove = dict(random.sample(pixmap, int(len(pixels) / 2))) result = (list(pixels), [white] * len(pixels)) #debug('result: %s' % repr(result)) for i in range(len(pixels)): if remove.has_key(i): result[1][i], result[0][i] = result[0][i], result[1][i] return (result[0], result[1]) def debug(message): if DEBUGGING: print >>sys.stderr, message if __name__ == '__main__': crosseyed(sys.argv[1], sys.argv[2])