Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 static function to parse extrema tuple arguments, and a new C function was added to calculate image entropy, making use of the new static extrema function. The extrema-parsing function was written by @homm, based on the macro abstraction I wrote, during the discussion of my first entropy-method pull request: https://git.io/fhodS 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. Changes proposed in this pull request: * Added “math.h” include to _imaging.c * The addition of an `image.entropy()` method to the `Image` Python class, * The abstraction of the extrema-parsing logic of of the C function `_histogram` into a static function, and * The use of that static function in both the `_histogram` and `_entropy` C functions. * Minor documentation addenda in the docstrings for both the `image.entropy()` and `image.histogram()` methods were also added.
- Loading branch information
Showing
4 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from helper import unittest, PillowTestCase, hopper | ||
|
||
|
||
class TestImageEntropy(PillowTestCase): | ||
|
||
def test_entropy(self): | ||
|
||
def entropy(mode): | ||
return hopper(mode).entropy() | ||
|
||
self.assertAlmostEqual(entropy("1"), 0.9138803254693582) | ||
self.assertAlmostEqual(entropy("L"), 7.06650513081286) | ||
self.assertAlmostEqual(entropy("I"), 7.06650513081286) | ||
self.assertAlmostEqual(entropy("F"), 7.06650513081286) | ||
self.assertAlmostEqual(entropy("P"), 5.0530452472519745) | ||
self.assertAlmostEqual(entropy("RGB"), 8.821286587714319) | ||
self.assertAlmostEqual(entropy("RGBA"), 7.42724306524488) | ||
self.assertAlmostEqual(entropy("CMYK"), 7.4272430652448795) | ||
self.assertAlmostEqual(entropy("YCbCr"), 7.698360534903628) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters