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

Conversation

fish2000
Copy link

As of version 6.1.0, the Pillow imaging library’s C module includes an optimized native entropy method:

https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#610-2019-07-01

This change detects and selects this new native method, if it is available in the version of Pillow upon which easy-thumbnails finds itself running.

Additionally, an optimized version of the existing Python image-entropy method is furnished as a fallback: rather than summing the entire histogram value set, this new function calculates the product of the image dimensions and band count, arriving at the same number. The new function also uses generator expressions and the specialized math.fsum(…) and math.log2(…) library calls to improve on the performance of its predecessor without altering the algorithm.

The appropriate image-entropy function is then conditionally ensconced in the easy_thumbnails.utils module, at module-load time.

I also made a slight tweak to the pil_image function, found in easy_thumbnails.source_generators – the call to PIL.Image.Image.load() was raising the exception noted in comments in that functions’ source while I was running the testsuite; now all such calls have these exceptions swallowed and everything works OK.

fish2000 and others added 2 commits November 13, 2019 14:24
As of version 6.1.0, the Pillow imaging library’s C module includes an optimized native entropy method:

• https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#610-2019-07-01

This change detects and selects this new native method, if it is available in the version of Pillow upon which `easy-thumbnails` finds itself running.

Additionally, an optimized version of the existing Python image-entropy method is furnished as a fallback: rather than summing the entire histogram value set, the function calculates the product of the image dimensions and band count to arrive at the same number. It also uses generator expressions and the specialized `math.fsum(…)` and `math.log2(…)` library calls to improve on the performance of its predecessor without altering the algorithm.

The appropriate image-entropy function is then conditionally ensconced in the `easy_thumbnails.utils` module, at module-load time.

I also made a slight tweak to the `pil_image` function, found in `easy_thumbnails.source_generators` – the call to `PIL.Image.Image.load()` was raising the exception noted in comments in that functions’ source while I was running the testsuite; now all such calls have these exceptions swallowed and everything works OK.

# 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 \


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

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?


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants