2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
from .helper import hopper
|
Added an `image.entropy()` method
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.
* Removed outdated boilerplate from testing code
* Removed unused “unittest” import
2019-01-25 20:38:11 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_entropy() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
def entropy(mode):
|
|
|
|
return hopper(mode).entropy()
|
Added an `image.entropy()` method
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.
* Removed outdated boilerplate from testing code
* Removed unused “unittest” import
2019-01-25 20:38:11 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert round(abs(entropy("1") - 0.9138803254693582), 7) == 0
|
|
|
|
assert round(abs(entropy("L") - 7.063008716585465), 7) == 0
|
|
|
|
assert round(abs(entropy("I") - 7.063008716585465), 7) == 0
|
|
|
|
assert round(abs(entropy("F") - 7.063008716585465), 7) == 0
|
2022-06-18 11:09:41 +03:00
|
|
|
assert round(abs(entropy("P") - 5.082506854662517), 7) == 0
|
2020-02-12 19:29:19 +03:00
|
|
|
assert round(abs(entropy("RGB") - 8.821286587714319), 7) == 0
|
|
|
|
assert round(abs(entropy("RGBA") - 7.42724306524488), 7) == 0
|
|
|
|
assert round(abs(entropy("CMYK") - 7.4272430652448795), 7) == 0
|
|
|
|
assert round(abs(entropy("YCbCr") - 7.698360534903628), 7) == 0
|