|
0 |
#!/usr/bin/env python
|
|
1 |
|
|
2 |
import os
|
|
3 |
import random
|
|
4 |
import sys
|
|
5 |
|
|
6 |
from PIL import Image
|
|
7 |
|
|
8 |
|
|
9 |
def main(argv):
|
|
10 |
base_filename = argv[1]
|
|
11 |
cutup_filenames = argv[2:]
|
|
12 |
|
|
13 |
base_image = Image.open(base_filename)
|
|
14 |
base_width = base_image.size[0]
|
|
15 |
base_height = base_image.size[1]
|
|
16 |
print base_image
|
|
17 |
|
|
18 |
images = [base_image]
|
|
19 |
for filename in cutup_filenames:
|
|
20 |
image = Image.open(filename)
|
|
21 |
# hope the aspect ratio is similar...
|
|
22 |
image = image.resize((base_width, base_height))
|
|
23 |
print image
|
|
24 |
images.append(image)
|
|
25 |
|
|
26 |
for n in xrange(0, 5000):
|
|
27 |
image = random.choice(images)
|
|
28 |
|
|
29 |
crop_width = int(base_width / (2 + (random.random() * 3)))
|
|
30 |
crop_height = int(base_height / 80.0)
|
|
31 |
|
|
32 |
# note that for crop boxes, the 3rd and 4th elements are
|
|
33 |
# *positions*, not *dimensions*!
|
|
34 |
crop_x = random.randint(0, base_width - crop_width)
|
|
35 |
crop_y = random.randint(0, base_height - crop_height)
|
|
36 |
crop_box = (
|
|
37 |
crop_x, crop_y, crop_x + crop_width, crop_y + crop_height
|
|
38 |
)
|
|
39 |
crop_region = image.crop(crop_box)
|
|
40 |
print image, crop_box, crop_region
|
|
41 |
|
|
42 |
paste_point = (random.randint(0, base_width - crop_width),
|
|
43 |
random.randint(0, base_height - crop_height))
|
|
44 |
|
|
45 |
base_image.paste(crop_region, paste_point)
|
|
46 |
|
|
47 |
print "Writing output.png..."
|
|
48 |
base_image.save("output.png")
|
|
49 |
|
|
50 |
|
|
51 |
if __name__ == '__main__':
|
|
52 |
import sys
|
|
53 |
main(sys.argv)
|