summaryrefslogblamecommitdiffstats
path: root/shred.py
blob: 40face9151fd9afa71f5f7d69017f8e3c24e6797 (plain) (tree)




























                                                                                                             
#!/usr/bin/env python

# Instagram Shredder
# by Jason A. Donenfeld <Jason@zx2c4.com>
# for http://instagram-engineering.tumblr.com/post/12651721845/instagram-engineering-challenge-the-unshredder

from PIL import Image
from random import shuffle
from sys import argv, exit, stderr

if len(argv) < 4:
	print >> stderr, "Usage: %s columns input output" % argv[0]
	exit(-1)

columns = int(argv[1])
image = Image.open(argv[2])
shredded = Image.new("RGBA", image.size)
width, height = image.size
shred_width = width / columns
sequence = range(0, columns)
shuffle(sequence)

for i, shred_index in enumerate(sequence):
	shred_x1, shred_y1 = shred_width * shred_index, 0
	shred_x2, shred_y2 = shred_x1 + shred_width, height
	region = image.crop((shred_x1, shred_y1, shred_x2, shred_y2))
	shredded.paste(region, (shred_width * i, 0))

shredded.save(argv[3])