-
Notifications
You must be signed in to change notification settings - Fork 323
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
base: master
Are you sure you want to change the base?
Conversation
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') \ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
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(…)
andmath.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 ineasy_thumbnails.source_generators
– the call toPIL.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.