Skip to content

Commit 7818abc

Browse files
committed
Refactor test_consistency; add 16bpc test
1 parent 2999ba4 commit 7818abc

1 file changed

Lines changed: 62 additions & 26 deletions

File tree

Tests/test_image_filter.py

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,36 +170,72 @@ def test_kernel_not_enough_coefficients() -> None:
170170
ImageFilter.Kernel((3, 3), (0, 0))
171171

172172

173-
def test_consistency_3x3() -> None:
174-
with Image.open("Tests/images/hopper.bmp") as source:
175-
with Image.open("Tests/images/hopper_emboss.bmp") as reference:
176-
kernel = ImageFilter.Kernel(
177-
(3, 3),
178-
# fmt: off
179-
(-1, -1, 0,
180-
-1, 0, 1,
181-
0, 1, 1),
182-
# fmt: on
183-
0.3,
184-
)
185-
assert_image_equal(source.filter(kernel), reference)
173+
# fmt: off
174+
EMBOSS_3x3 = (
175+
-1, -1, 0,
176+
-1, 0, 1,
177+
0, 1, 1,
178+
)
179+
EMBOSS_5x5 = (
180+
-1, -1, -1, -1, 0,
181+
-1, -1, -1, 0, 1,
182+
-1, -1, 0, 1, 1,
183+
-1, 0, 1, 1, 1,
184+
0, 1, 1, 1, 1,
185+
)
186+
# fmt: on
186187

187188

188-
def test_consistency_5x5() -> None:
189+
@pytest.mark.parametrize(
190+
"size, weights, expected",
191+
[
192+
pytest.param(
193+
(3, 3),
194+
EMBOSS_3x3,
195+
"Tests/images/hopper_emboss.bmp",
196+
id="3x3",
197+
),
198+
pytest.param(
199+
(5, 5), EMBOSS_5x5, "Tests/images/hopper_emboss_more.bmp", id="5x5"
200+
),
201+
],
202+
)
203+
def test_consistency(
204+
size: tuple[int, int], weights: tuple[int, ...], expected: str
205+
) -> None:
189206
with Image.open("Tests/images/hopper.bmp") as source:
190-
with Image.open("Tests/images/hopper_emboss_more.bmp") as reference:
191-
kernel = ImageFilter.Kernel(
192-
(5, 5),
193-
# fmt: off
194-
(-1, -1, -1, -1, 0,
195-
-1, -1, -1, 0, 1,
196-
-1, -1, 0, 1, 1,
197-
-1, 0, 1, 1, 1,
198-
0, 1, 1, 1, 1),
199-
# fmt: on
200-
0.3,
207+
with Image.open(expected) as reference:
208+
kernel = ImageFilter.Kernel(size, weights, 0.3)
209+
result = source.filter(kernel)
210+
assert_image_equal(result, reference)
211+
212+
213+
@pytest.mark.parametrize(
214+
"size, weights",
215+
[
216+
pytest.param((3, 3), EMBOSS_3x3, id="3x3"),
217+
pytest.param((5, 5), EMBOSS_5x5, id="5x5"),
218+
],
219+
)
220+
@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N"))
221+
def test_consistency_i16(
222+
size: tuple[int, int], weights: tuple[int, ...], mode: str
223+
) -> None:
224+
kernel = ImageFilter.Kernel(size, weights, 0.3)
225+
reference = hopper("I").filter(kernel)
226+
result = hopper(mode).filter(kernel)
227+
assert result.mode == mode
228+
assert result.size == reference.size
229+
# Compare logical pixel values.
230+
assert (
231+
max(
232+
abs(a - b) # type: ignore[operator]
233+
for a, b in zip(
234+
reference.get_flattened_data(), result.get_flattened_data(), strict=True
201235
)
202-
assert_image_equal(source.filter(kernel), reference)
236+
)
237+
<= 1
238+
)
203239

204240

205241
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)