, solr-thumbnails, , reddit, , . , . , , , . reddit, , .
import Image, ImageFile, math
def image_entropy(im):
"""From Reddit: Calculate the entropy of an image"""
hist = im.histogram()
hist_size = sum(hist)
hist = [float(h) / hist_size for h in hist]
return -sum([p * math.log(p, 2) for p in hist if p != 0])
def square_image(im, requested_size, opts):
"""From Reddit: if the image is taller than it is wide, square it off. determine
which pieces to cut off based on the entropy pieces.
This version is improved as it squares images that are wider than it is tall.
"""
if 'autosquare' in opts:
x,y = im.size
if y > x:
while y > x:
slice_height = min(y - x, 10)
bottom = im.crop((0, y - slice_height, x, y))
top = im.crop((0, 0, x, slice_height))
if image_entropy(bottom) < image_entropy(top):
im = im.crop((0, 0, x, y - slice_height))
else:
im = im.crop((0, slice_height, x, y))
x,y = im.size
else:
while y < x:
slice_width = min(x - y, 10)
left = im.crop((0,0, y, slice_width))
right = im.crop((0,y - slice_width, x, y))
if image_entropy(left) < image_entropy(right):
im = im.crop((0, 0, x - slice_width, y))
else:
im = im.crop((slice_width, 0, x, y))
x,y = im.size
im = im.resize(requested_size, resample=Image.ANTIALIAS)
return im
square_image.valid_options = ('autosquare',)