-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses one of the issues raised by in @madisvain in #93
- Loading branch information
1 parent
a041302
commit 09b97ee
Showing
4 changed files
with
96 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters