Added image.entropy() method#3530
Conversation
|
The tests are failing, due to float-comparison issues… I will rewrite the test code to deal with that and re-push. |
5300972 to
a3b6bd4
Compare
|
I’m on Mac OS X, and I have no idea how to fix the Appveyor build scripts – can someone who knows windows help me out, or point me in the right direction? Evidently it needs to link the C math library when linking the result of compiling |
This calculates the entropy for the image, based on the histogram.
Because this uses image histogram data directly, the existing C
function underpinning the `image.histogram()` method was abstracted
into a macro, and a new C function was added that uses this macro.
The new `image.entropy()` method is based on `image.histogram()`,
and will accept the same arguments to calculate the histogram
data it will use to assess the entropy of the image.
The algorithm and methodology is based on existing Python code:
https://git.io/fhmIU
... A test case in the `Tests/` directory, and doctest lines in
`selftest.py`, have both been added and checked.
Subsequent commits:
* Using assertAlmostEqual() in entropy tests
* Added description of `extrema` arguments.
* Only test seven digits of float returned by im.entropy()
a3b6bd4 to
4ce620c
Compare
|
I have created fish2000#1 |
Removed log2
|
Any reason to implement it in C rather than in Python? I'm comparing with this implementation: def entr(hist):
from math import log
fs = 1.0 / sum(hist)
fentropy = 0.0
for h in hist:
if h != 0:
h = h * fs
fentropy += h * log(h, 2)
return -fentropyFor 1024 × 640 image I get: The bigger image, the smaller the difference between Python and C implementation. Does it worth it? |
Short answer: yes, I believe so. Many histogram-entropy functions are implemented in Python, using the PIL/Pillow
One of PIL/Pillow’s primary uses is in web applications – processing images uploaded by users, avatar images, Here’s the long answer: the case I would make for moving this algorithm into Pillow’s Using a C function, utilizing the Pillow internal structures (e.g. |
This calculates the entropy for the image, based on the histogram.
Because this uses image histogram data directly, the existing C function underpinning the
image.histogram()method was abstracted into a macro, and a new C function was added that uses this macro.The new
image.entropy()method is based onimage.histogram(), and will accept the same arguments to calculate the histogram data it will use to assess the entropy of the image.The algorithm and methodology is based on existing Python code:
... A test case in the
Tests/directory, and doctest lines inselftest.py, have both been added and checked.Changes proposed in this pull request:
image.entropy()method,_histograminto a macro, and_histogramand_entropy.image.entropy()andimage.histogram()