-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathtest_image_filter.py
More file actions
258 lines (222 loc) · 6.93 KB
/
Copy pathtest_image_filter.py
File metadata and controls
258 lines (222 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from __future__ import annotations
import pytest
from PIL import Image, ImageFilter
from .helper import assert_image_equal, hopper
MODES = ("L", "LA", "I", "I;16", "I;16L", "I;16B", "I;16N", "RGB", "CMYK")
@pytest.mark.parametrize(
"filter_to_apply",
(
ImageFilter.BLUR,
ImageFilter.CONTOUR,
ImageFilter.DETAIL,
ImageFilter.EDGE_ENHANCE,
ImageFilter.EDGE_ENHANCE_MORE,
ImageFilter.EMBOSS,
ImageFilter.FIND_EDGES,
ImageFilter.SMOOTH,
ImageFilter.SMOOTH_MORE,
ImageFilter.SHARPEN,
ImageFilter.MaxFilter,
ImageFilter.MedianFilter,
ImageFilter.MinFilter,
ImageFilter.ModeFilter,
ImageFilter.GaussianBlur,
ImageFilter.GaussianBlur(0),
ImageFilter.GaussianBlur(5),
ImageFilter.GaussianBlur((2, 5)),
ImageFilter.BoxBlur(0),
ImageFilter.BoxBlur(5),
ImageFilter.BoxBlur((2, 5)),
ImageFilter.UnsharpMask,
ImageFilter.UnsharpMask(10),
),
)
@pytest.mark.parametrize("mode", MODES)
def test_sanity(
filter_to_apply: ImageFilter.Filter | type[ImageFilter.Filter], mode: str
) -> None:
im = hopper(mode)
if mode[0] != "I" or (
callable(filter_to_apply)
and issubclass(filter_to_apply, ImageFilter.BuiltinFilter)
):
out = im.filter(filter_to_apply)
assert out.mode == im.mode
assert out.size == im.size
@pytest.mark.parametrize("mode", MODES)
def test_sanity_error(mode: str) -> None:
im = hopper(mode)
with pytest.raises(TypeError):
im.filter("hello") # type: ignore[arg-type]
@pytest.mark.parametrize("size", ((1, 1), (2, 2), (3, 3)))
def test_noop_on_small_images(size: tuple[int, int]) -> None:
# If image is smaller than kernel size, return it as-is
im = Image.new("RGB", size)
assert_image_equal(im, im.filter(ImageFilter.SMOOTH))
@pytest.mark.parametrize(
"mode, expected",
(
("1", (4, 0)),
("L", (4, 0)),
("P", (4, 0)),
("RGB", ((4, 0, 0), (0, 0, 0))),
),
)
def test_modefilter(
mode: str,
expected: tuple[int, int] | tuple[tuple[int, int, int], tuple[int, int, int]],
) -> None:
im = Image.new(mode, (3, 3), None)
im.putdata(list(range(9)))
# image is:
# 0 1 2
# 3 4 5
# 6 7 8
mod = im.filter(ImageFilter.ModeFilter).getpixel((1, 1))
im.putdata([0, 0, 1, 2, 5, 1, 5, 2, 0]) # mode=0
mod2 = im.filter(ImageFilter.ModeFilter).getpixel((1, 1))
assert (mod, mod2) == expected
@pytest.mark.parametrize(
"mode, expected",
(
("1", (0, 4, 8)),
("L", (0, 4, 8)),
("RGB", ((0, 0, 0), (4, 0, 0), (8, 0, 0))),
("I", (0, 4, 8)),
("F", (0.0, 4.0, 8.0)),
),
)
def test_rankfilter(
mode: str,
expected: (
tuple[float, float, float]
| tuple[tuple[int, int, int], tuple[int, int, int], tuple[int, int, int]]
),
) -> None:
im = Image.new(mode, (3, 3), None)
im.putdata(list(range(9)))
# image is:
# 0 1 2
# 3 4 5
# 6 7 8
minimum = im.filter(ImageFilter.MinFilter).getpixel((1, 1))
med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1))
maximum = im.filter(ImageFilter.MaxFilter).getpixel((1, 1))
assert (minimum, med, maximum) == expected
@pytest.mark.parametrize(
"filter", (ImageFilter.MinFilter, ImageFilter.MedianFilter, ImageFilter.MaxFilter)
)
def test_rankfilter_error(filter: ImageFilter.RankFilter) -> None:
with pytest.raises(ValueError):
im = Image.new("P", (3, 3), None)
im.putdata(list(range(9)))
# image is:
# 0 1 2
# 3 4 5
# 6 7 8
im.filter(filter).getpixel((1, 1))
def test_rankfilter_properties() -> None:
rankfilter = ImageFilter.RankFilter(3, 2)
assert rankfilter.size == 3
assert rankfilter.rank == 2
with pytest.raises(ValueError, match="bad filter size"):
ImageFilter.RankFilter(2, 1)
with pytest.raises(ValueError, match="bad filter size"):
ImageFilter.MaxFilter(2)
with pytest.raises(ValueError, match="bad filter size"):
ImageFilter.MedianFilter(2)
with pytest.raises(ValueError, match="bad filter size"):
ImageFilter.MinFilter(2)
with pytest.raises(ValueError, match="filter size too large"):
ImageFilter.RankFilter(23171, 1)
im = Image.new("1", (1, 1))
with pytest.raises(ValueError, match="filter size too large"):
im.im.expand(23171)
with pytest.raises(ValueError, match="bad rank value"):
ImageFilter.RankFilter(1, 1)
def test_builtinfilter_p() -> None:
builtin_filter = ImageFilter.BuiltinFilter()
with pytest.raises(ValueError):
builtin_filter.filter(hopper("P").im)
def test_kernel_not_enough_coefficients() -> None:
with pytest.raises(ValueError):
ImageFilter.Kernel((3, 3), (0, 0))
# fmt: off
EMBOSS_3x3 = (
-1, -1, 0,
-1, 0, 1,
0, 1, 1,
)
EMBOSS_5x5 = (
-1, -1, -1, -1, 0,
-1, -1, -1, 0, 1,
-1, -1, 0, 1, 1,
-1, 0, 1, 1, 1,
0, 1, 1, 1, 1,
)
# fmt: on
@pytest.mark.parametrize(
"size, weights, expected",
[
pytest.param(
(3, 3),
EMBOSS_3x3,
"Tests/images/hopper_emboss.bmp",
id="3x3",
),
pytest.param(
(5, 5), EMBOSS_5x5, "Tests/images/hopper_emboss_more.bmp", id="5x5"
),
],
)
def test_consistency(
size: tuple[int, int], weights: tuple[int, ...], expected: str
) -> None:
with Image.open("Tests/images/hopper.bmp") as source:
with Image.open(expected) as reference:
kernel = ImageFilter.Kernel(size, weights, 0.3)
result = source.filter(kernel)
assert_image_equal(result, reference)
@pytest.mark.parametrize(
"size, weights",
[
pytest.param((3, 3), EMBOSS_3x3, id="3x3"),
pytest.param((5, 5), EMBOSS_5x5, id="5x5"),
],
)
@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N"))
def test_consistency_i16(
size: tuple[int, int], weights: tuple[int, ...], mode: str
) -> None:
kernel = ImageFilter.Kernel(size, weights, 0.3)
reference = hopper("I").filter(kernel)
result = hopper(mode).filter(kernel)
assert result.mode == mode
assert result.size == reference.size
# Compare logical pixel values.
assert (
max(
abs(a - b) # type: ignore[operator]
for a, b in zip(
reference.get_flattened_data(), result.get_flattened_data(), strict=True
)
)
<= 1
)
@pytest.mark.parametrize(
"radius",
(
-2,
(-2, -2),
(-2, 2),
(2, -2),
),
)
def test_invalid_box_blur_filter(radius: int | tuple[int, int]) -> None:
with pytest.raises(ValueError):
ImageFilter.BoxBlur(radius)
im = hopper()
box_blur_filter = ImageFilter.BoxBlur(2)
box_blur_filter.radius = radius
with pytest.raises(ValueError):
im.filter(box_blur_filter)