2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImageStat
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
from .helper import hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
def test_sanity():
|
|
|
|
im = hopper()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
st = ImageStat.Stat(im)
|
|
|
|
st = ImageStat.Stat(im.histogram())
|
|
|
|
st = ImageStat.Stat(im, Image.new("1", im.size, 1))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
# Check these run. Exceptions will cause failures.
|
|
|
|
st.extrema
|
|
|
|
st.sum
|
|
|
|
st.mean
|
|
|
|
st.median
|
|
|
|
st.rms
|
|
|
|
st.sum2
|
|
|
|
st.var
|
|
|
|
st.stddev
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
st.spam()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
ImageStat.Stat(1)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
def test_hopper():
|
|
|
|
im = hopper()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
st = ImageStat.Stat(im)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
# verify a few values
|
|
|
|
assert st.extrema[0] == (0, 255)
|
|
|
|
assert st.median[0] == 72
|
|
|
|
assert st.sum[0] == 1470218
|
|
|
|
assert st.sum[1] == 1311896
|
|
|
|
assert st.sum[2] == 1563008
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
def test_constant():
|
|
|
|
im = Image.new("L", (128, 128), 128)
|
|
|
|
|
|
|
|
st = ImageStat.Stat(im)
|
|
|
|
|
|
|
|
assert st.extrema[0] == (128, 128)
|
2022-03-04 08:42:24 +03:00
|
|
|
assert st.sum[0] == 128**3
|
|
|
|
assert st.sum2[0] == 128**4
|
2019-11-04 01:18:55 +03:00
|
|
|
assert st.mean[0] == 128
|
|
|
|
assert st.median[0] == 128
|
|
|
|
assert st.rms[0] == 128
|
|
|
|
assert st.var[0] == 0
|
|
|
|
assert st.stddev[0] == 0
|