summaryrefslogtreecommitdiffstats
path: root/shred.py
blob: 40face9151fd9afa71f5f7d69017f8e3c24e6797 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/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])