Updating image entropy implementations #529
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.