Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating image entropy implementations #529

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 31 additions & 9 deletions easy_thumbnails/utils.py
@@ -1,24 +1,46 @@
import hashlib
import inspect
import math

from django.utils import six
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use six

from django.utils.functional import LazyObject
from django.utils import timezone
from PIL import Image

try:
from PIL import Image, ImageMode
except ImportError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is non need for this except

import Image, ImageMode

from easy_thumbnails.conf import settings


def image_entropy(im):
def color_count(image):
"""
Return the number of color values in the input image --
this is the number of pixels times the band count of the image.
"""
mode_descriptor = ImageMode.getmode(image.mode)
width, height = image.size
return width * height * len(mode_descriptor.bands)


def image_entropy_py(image):
"""
Calculate the entropy of an image. Used for "smart cropping".
Calculate the entropy of an images' histogram.
"""
if not isinstance(im, Image.Image):
if not isinstance(image, Image.Image):
# Can only deal with PIL images. Fall back to a constant entropy.
return 0
hist = im.histogram()
hist_size = float(sum(hist))
hist = [h / hist_size for h in hist]
return -sum([p * math.log(p, 2) for p in hist if p != 0])
from math import log2, fsum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why import is local?

histosum = float(color_count(image))
histonorm = (histocol / histosum for histocol in image.histogram())
return -fsum(p * log2(p) for p in histonorm if p != 0.0)


# Select the Pillow native image entropy function - if available -
# and fall back to our Python implementation:
image_entropy = hasattr(Image.Image, 'entropy') \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't break lines with \

and Image.Image.entropy \
or image_entropy_py


def dynamic_import(import_string):
Expand Down