memonic

Reddit image squaring algorithm

Save
64 def square_image(img):
65     """if the image is taller than it is wide, square it off. determine
66     which pieces to cut off based on the entropy pieces."""
67     x,y = img.size
68     while y > x:
69         #slice 10px at a time until square
70         slice_height = min(y - x, 10)
71
72         bottom = img.crop((0, y - slice_height, x, y))
73         top = img.crop((0, 0, x, slice_height))
74
75         #remove the slice with the least entropy
76         if image_entropy(bottom) < image_entropy(top):
77             img = img.crop((0, 0, x, y - slice_height))
78         else:
79             img = img.crop((0, slice_height, x, y))
80
81         x,y = img.size
82
83     return img