image_entropy.py 281 B

1234567891011
  1. import math
  2. def image_entropy(im):
  3. """
  4. Calculate the entropy of an image. Used for "smart cropping".
  5. """
  6. hist = im.histogram()
  7. hist_size = float(sum(hist))
  8. hist = [h / hist_size for h in hist]
  9. return -sum([p * math.log(p, 2) for p in hist if p != 0])