git @ Cat's Eye Technologies NaNoGenLab / f612982
Import of narrow-cut-up experiment. Chris Pressey 10 years ago
2 changed file(s) with 81 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 narrow-cut-up
1 =============
2
3 Requirements
4 ------------
5
6 * Python 2.7.6 (probably works with older versions too)
7 * ImageMagick
8 * [Pillow](http://python-pillow.github.io/) (it might work with PIL too)
9 * Some scanned images of newspapers, books, etc.
10
11 Basic Strategy
12 --------------
13
14 * Like [naive-cut-up](../naive-cut-up), except that it:
15 * scales each image to the size of the base ("canvas") image
16 * cuts and pastes in narrow strips instead of 1/9-page chunks
17
18 Usage
19 -----
20
21 We can use images fetched by naive-cut-up.
22
23 $ ./narrow-cut-up.py ../naive-cut-up/ca_5.jp2.png ../naive-cut-up/pages/*
24
25 The result has promise, but looks like it might work best with pages
26 which contain more text than images. Screenshot forthcoming.
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)