django-imagekit/imagekit/processors/utils.py
Matthew Tretter 09b97ee62f Move SmartCrop to crop module
Addresses one of the issues raised by in @madisvain in #93
2012-02-10 19:42:36 -05:00

18 lines
574 B
Python

import math
from imagekit.lib import Image
def histogram_entropy(im):
"""
Calculate the entropy of an images' histogram. Used for "smart cropping" in easy-thumbnails;
see: https://raw.github.com/SmileyChris/easy-thumbnails/master/easy_thumbnails/utils.py
"""
if not isinstance(im, Image.Image):
return 0 # Fall back to a constant entropy.
histogram = im.histogram()
hist_ceil = float(sum(histogram))
histonorm = [histocol / hist_ceil for histocol in histogram]
return -sum([p * math.log(p, 2) for p in histonorm if p != 0])