git @ Cat's Eye Technologies NaNoGenLab / 2071eb4
Definitely showing promise, but needs a --debug option I think. Chris Pressey 10 years ago
1 changed file(s) with 42 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
1616
1717 (width, height) = image.size
1818
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
1922 margin_left = int(width * 0.05)
2023 margin_right = int(width * 0.95)
2124 margin_top = int(height * 0.05)
2730 (width, height) = image.size
2831
2932 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
3037
3138 for y in xrange(0, height):
3239 if y % 100 == 0:
6471 previous_light_y = None
6572 cuttable_ribbons.append((start_y, thickness))
6673
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?
7182
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")
73110
74111
75112 if __name__ == '__main__':