Definitely showing promise, but needs a --debug option I think.
Chris Pressey
10 years ago
16 | 16 | |
17 | 17 | (width, height) = image.size |
18 | 18 | |
19 | # FIXME: iteratively look for too-dark rectangles around the edge of | |
20 | # the image and remove them one by one instead. but this is OK for now | |
21 | ||
19 | 22 | margin_left = int(width * 0.05) |
20 | 23 | margin_right = int(width * 0.95) |
21 | 24 | margin_top = int(height * 0.05) |
27 | 30 | (width, height) = image.size |
28 | 31 | |
29 | 32 | light_rows = [] |
33 | ||
34 | # FIXME: throw the image through an (almost-)max-contrast filter first; | |
35 | # that should make it easier to work on a wide range of images of varying | |
36 | # contrast levels | |
30 | 37 | |
31 | 38 | for y in xrange(0, height): |
32 | 39 | if y % 100 == 0: |
64 | 71 | previous_light_y = None |
65 | 72 | cuttable_ribbons.append((start_y, thickness)) |
66 | 73 | |
67 | for ribbon in cuttable_ribbons: | |
68 | for y in xrange(ribbon[0], ribbon[0] + ribbon[1]): | |
69 | for x in xrange(0, width): | |
70 | image.putpixel((x, y), 0) | |
74 | # reduce ribbon thicknesses | |
75 | margin = 4 # how much whitespace you want around darkpixelness? | |
76 | cuttable_ribbons = [ | |
77 | (start_y + margin, thickness - margin * 2) | |
78 | for (start_y, thickness) in cuttable_ribbons | |
79 | if thickness > margin * 2 | |
80 | ] | |
81 | # FIXME: we could / should retain the insufficiently-thick ones? | |
71 | 82 | |
72 | image.save("output.png") | |
83 | if False: | |
84 | print cuttable_ribbons | |
85 | for ribbon in cuttable_ribbons: | |
86 | for y in xrange(ribbon[0], ribbon[0] + ribbon[1]): | |
87 | for x in xrange(0, width): | |
88 | image.putpixel((x, y), 0) | |
89 | ||
90 | # compute the crop-areas BETWEEN the cuttable ribbons | |
91 | crop_y = 0 | |
92 | crop_areas = [] | |
93 | ||
94 | for (start_y, thickness) in cuttable_ribbons: | |
95 | crop_areas.append( | |
96 | (0, crop_y, width, start_y) | |
97 | ) | |
98 | crop_y = start_y + thickness | |
99 | ||
100 | crop_areas.append( | |
101 | (0, crop_y, width, height) | |
102 | ) | |
103 | ||
104 | for (crop_num, crop_area) in enumerate(crop_areas): | |
105 | region = image.crop(crop_area) | |
106 | print "writing %s to crop%s.png" % (crop_area, crop_num) | |
107 | region.save("crop%s.png" % crop_num) | |
108 | ||
109 | #image.save("output.png") | |
73 | 110 | |
74 | 111 | |
75 | 112 | if __name__ == '__main__': |