/
test_imagechops.py
428 lines (299 loc) · 10.4 KB
/
test_imagechops.py
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from PIL import Image, ImageChops
from .helper import assert_image_equal, hopper
BLACK = (0, 0, 0)
BROWN = (127, 64, 0)
CYAN = (0, 255, 255)
DARK_GREEN = (0, 128, 0)
GREEN = (0, 255, 0)
ORANGE = (255, 128, 0)
WHITE = (255, 255, 255)
GREY = 128
def test_sanity():
im = hopper("L")
ImageChops.constant(im, 128)
ImageChops.duplicate(im)
ImageChops.invert(im)
ImageChops.lighter(im, im)
ImageChops.darker(im, im)
ImageChops.difference(im, im)
ImageChops.multiply(im, im)
ImageChops.screen(im, im)
ImageChops.add(im, im)
ImageChops.add(im, im, 2.0)
ImageChops.add(im, im, 2.0, 128)
ImageChops.subtract(im, im)
ImageChops.subtract(im, im, 2.0)
ImageChops.subtract(im, im, 2.0, 128)
ImageChops.add_modulo(im, im)
ImageChops.subtract_modulo(im, im)
ImageChops.blend(im, im, 0.5)
ImageChops.composite(im, im, im)
ImageChops.soft_light(im, im)
ImageChops.hard_light(im, im)
ImageChops.overlay(im, im)
ImageChops.offset(im, 10)
ImageChops.offset(im, 10, 20)
def test_add():
# Arrange
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.add(im1, im2)
# Assert
assert new.getbbox() == (25, 25, 76, 76)
assert new.getpixel((50, 50)) == ORANGE
def test_add_scale_offset():
# Arrange
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.add(im1, im2, scale=2.5, offset=100)
# Assert
assert new.getbbox() == (0, 0, 100, 100)
assert new.getpixel((50, 50)) == (202, 151, 100)
def test_add_clip():
# Arrange
im = hopper()
# Act
new = ImageChops.add(im, im)
# Assert
assert new.getpixel((50, 50)) == (255, 255, 254)
def test_add_modulo():
# Arrange
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.add_modulo(im1, im2)
# Assert
assert new.getbbox() == (25, 25, 76, 76)
assert new.getpixel((50, 50)) == ORANGE
def test_add_modulo_no_clip():
# Arrange
im = hopper()
# Act
new = ImageChops.add_modulo(im, im)
# Assert
assert new.getpixel((50, 50)) == (224, 76, 254)
def test_blend():
# Arrange
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.blend(im1, im2, 0.5)
# Assert
assert new.getbbox() == (25, 25, 76, 76)
assert new.getpixel((50, 50)) == BROWN
def test_constant():
# Arrange
im = Image.new("RGB", (20, 10))
# Act
new = ImageChops.constant(im, GREY)
# Assert
assert new.size == im.size
assert new.getpixel((0, 0)) == GREY
assert new.getpixel((19, 9)) == GREY
def test_darker_image():
# Arrange
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.darker(im1, im2)
# Assert
assert_image_equal(new, im2)
def test_darker_pixel():
# Arrange
im1 = hopper()
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.darker(im1, im2)
# Assert
assert new.getpixel((50, 50)) == (240, 166, 0)
def test_difference():
# Arrange
with Image.open("Tests/images/imagedraw_arc_end_le_start.png") as im1:
with Image.open("Tests/images/imagedraw_arc_no_loops.png") as im2:
# Act
new = ImageChops.difference(im1, im2)
# Assert
assert new.getbbox() == (25, 25, 76, 76)
def test_difference_pixel():
# Arrange
im1 = hopper()
with Image.open("Tests/images/imagedraw_polygon_kite_RGB.png") as im2:
# Act
new = ImageChops.difference(im1, im2)
# Assert
assert new.getpixel((50, 50)) == (240, 166, 128)
def test_duplicate():
# Arrange
im = hopper()
# Act
new = ImageChops.duplicate(im)
# Assert
assert_image_equal(new, im)
def test_invert():
# Arrange
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im:
# Act
new = ImageChops.invert(im)
# Assert
assert new.getbbox() == (0, 0, 100, 100)
assert new.getpixel((0, 0)) == WHITE
assert new.getpixel((50, 50)) == CYAN
def test_lighter_image():
# Arrange
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.lighter(im1, im2)
# Assert
assert_image_equal(new, im1)
def test_lighter_pixel():
# Arrange
im1 = hopper()
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.lighter(im1, im2)
# Assert
assert new.getpixel((50, 50)) == (255, 255, 127)
def test_multiply_black():
"""If you multiply an image with a solid black image,
the result is black."""
# Arrange
im1 = hopper()
black = Image.new("RGB", im1.size, "black")
# Act
new = ImageChops.multiply(im1, black)
# Assert
assert_image_equal(new, black)
def test_multiply_green():
# Arrange
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im:
green = Image.new("RGB", im.size, "green")
# Act
new = ImageChops.multiply(im, green)
# Assert
assert new.getbbox() == (25, 25, 76, 76)
assert new.getpixel((25, 25)) == DARK_GREEN
assert new.getpixel((50, 50)) == BLACK
def test_multiply_white():
"""If you multiply with a solid white image, the image is unaffected."""
# Arrange
im1 = hopper()
white = Image.new("RGB", im1.size, "white")
# Act
new = ImageChops.multiply(im1, white)
# Assert
assert_image_equal(new, im1)
def test_offset():
# Arrange
xoffset = 45
yoffset = 20
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im:
# Act
new = ImageChops.offset(im, xoffset, yoffset)
# Assert
assert new.getbbox() == (0, 45, 100, 96)
assert new.getpixel((50, 50)) == BLACK
assert new.getpixel((50 + xoffset, 50 + yoffset)) == DARK_GREEN
# Test no yoffset
assert ImageChops.offset(im, xoffset) == ImageChops.offset(im, xoffset, xoffset)
def test_screen():
# Arrange
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.screen(im1, im2)
# Assert
assert new.getbbox() == (25, 25, 76, 76)
assert new.getpixel((50, 50)) == ORANGE
def test_subtract():
# Arrange
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract(im1, im2)
# Assert
assert new.getbbox() == (25, 50, 76, 76)
assert new.getpixel((50, 50)) == GREEN
assert new.getpixel((50, 51)) == BLACK
def test_subtract_scale_offset():
# Arrange
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract(im1, im2, scale=2.5, offset=100)
# Assert
assert new.getbbox() == (0, 0, 100, 100)
assert new.getpixel((50, 50)) == (100, 202, 100)
def test_subtract_clip():
# Arrange
im1 = hopper()
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract(im1, im2)
# Assert
assert new.getpixel((50, 50)) == (0, 0, 127)
def test_subtract_modulo():
# Arrange
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract_modulo(im1, im2)
# Assert
assert new.getbbox() == (25, 50, 76, 76)
assert new.getpixel((50, 50)) == GREEN
assert new.getpixel((50, 51)) == BLACK
def test_subtract_modulo_no_clip():
# Arrange
im1 = hopper()
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract_modulo(im1, im2)
# Assert
assert new.getpixel((50, 50)) == (241, 167, 127)
def test_soft_light():
# Arrange
im1 = Image.open("Tests/images/hopper.png")
im2 = Image.open("Tests/images/hopper-XYZ.png")
# Act
new = ImageChops.soft_light(im1, im2)
# Assert
assert new.getpixel((64, 64)) == (163, 54, 32)
assert new.getpixel((15, 100)) == (1, 1, 3)
def test_hard_light():
# Arrange
im1 = Image.open("Tests/images/hopper.png")
im2 = Image.open("Tests/images/hopper-XYZ.png")
# Act
new = ImageChops.hard_light(im1, im2)
# Assert
assert new.getpixel((64, 64)) == (144, 50, 27)
assert new.getpixel((15, 100)) == (1, 1, 2)
def test_overlay():
# Arrange
im1 = Image.open("Tests/images/hopper.png")
im2 = Image.open("Tests/images/hopper-XYZ.png")
# Act
new = ImageChops.overlay(im1, im2)
# Assert
assert new.getpixel((64, 64)) == (159, 50, 27)
assert new.getpixel((15, 100)) == (1, 1, 2)
def test_logical():
def table(op, a, b):
out = []
for x in (a, b):
imx = Image.new("1", (1, 1), x)
for y in (a, b):
imy = Image.new("1", (1, 1), y)
out.append(op(imx, imy).getpixel((0, 0)))
return tuple(out)
assert table(ImageChops.logical_and, 0, 1) == (0, 0, 0, 255)
assert table(ImageChops.logical_or, 0, 1) == (0, 255, 255, 255)
assert table(ImageChops.logical_xor, 0, 1) == (0, 255, 255, 0)
assert table(ImageChops.logical_and, 0, 128) == (0, 0, 0, 255)
assert table(ImageChops.logical_or, 0, 128) == (0, 255, 255, 255)
assert table(ImageChops.logical_xor, 0, 128) == (0, 255, 255, 0)
assert table(ImageChops.logical_and, 0, 255) == (0, 0, 0, 255)
assert table(ImageChops.logical_or, 0, 255) == (0, 255, 255, 255)
assert table(ImageChops.logical_xor, 0, 255) == (0, 255, 255, 0)