/
test_Goulib_image.py
644 lines (495 loc) · 20.7 KB
/
test_Goulib_image.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
from nose.tools import assert_equal
from nose import SkipTest
# lines above are inserted automatically by pythoscope. Line below overrides them
from Goulib.tests import * # pylint: disable=wildcard-import, unused-wildcard-import
from Goulib.image import * # pylint: disable=wildcard-import, unused-wildcard-import
from skimage import data
import os
path = os.path.dirname(os.path.abspath(__file__))
results = path+'\\results\\image\\' # path for results
def assert_image(image, name=None, convert=False):
""" Checks if an image is present (not black, white, or low contrast
:param image: Image to check
:param name: str, optional file name. image is saved is specified
:param convert: bool specifies if image is converted to RGB for saving
"""
from skimage.exposure import is_low_contrast
if name:
image.save(results+name, autoconvert=convert)
if is_low_contrast(image.array):
logging.warning('image %s has low contrast' % name)
class TestImage:
@classmethod
def setup_class(self):
self.lena = Image(path+'/data/lena.png')
assert_equal(self.lena, self.lena) # make sure image comparizon works
assert_image(self.lena.grayscale('L'),
'grayscale.png') # force to uint8
self.gray = Image(results+'grayscale.png')
self.camera = Image(data.camera())
def test_pdf(self):
return # for now for speed
try:
import pdfminer
except:
return # pass
assert_image(Image(path+'/data/lena.pdf'), 'pdf_out.png')
assert_image(Image(path+'/data/Pantone Fan.pdf'), 'Pantone Fan.png')
# Image(path+'/data/Pantone Fan CMYKOGB Equinox.pdf').save('Pantone Fan CMYKOGB Equinox.png')
def test___init__(self):
lena2 = Image(self.lena)
assert_equal(self.lena, lena2)
def test_generate(self):
# from matrix
from matplotlib import cm
a = [[-x*x+y*y for x in range(128)] for y in range(128)]
a = normalize(a)
assert_image(Image(a), 'generated.png')
assert_image(Image(a, colormap=cm.get_cmap('nipy_spectral')),
'gen_colormap.png', True)
def test___hash__(self):
h1 = hash(self.lena)
h2 = hash(self.gray)
diff = h1 ^ h2 # XOR
diff = math2.digsum(diff, 2) # number of different pixels
assert_equal(h1, h2, msg='difference is %d pixels' % diff)
def test_dist(self):
assert_equal(self.lena.dist(self.gray),0)
s=self.lena.size;
lena2=self.lena.resize((s[0],s[1]*2))
lena2.save(results+'lena.2.width.png')
tol=4/64 # don't know why...
d=self.lena.dist(lena2)
assert_true(d<=tol)
for method in [AVERAGE, PERCEPTUAL]:
assert_true(self.lena.dist(lena2,method)<=tol)
assert_true(self.lena.dist(lena2.flip(),method,symmetries=True)<=tol)
assert_true(self.lena.dist(lena2.flip(False,True),method,symmetries=True)<=tol)
def test___getitem__(self):
pixel = self.gray[256, 256]
assert_equal(pixel, 90)
pixel = self.lena[256, 256]
# (180,65,72))
assert_equal(pixel, [0.70588235, 0.25490196, 0.28235294])
left = self.lena[:, :256]
right = self.lena[:, 256:-1]
face = self.lena[246:374, 225:353]
assert_image(face, 'lena.face.png')
face = face.grayscale()
eye = face[3:35, -35:-3] # negative indexes are handy in some cases
c = face.correlation(eye)
# assert_image(c,"correlation.png")
def test___lt__(self):
# image = Image(data)
# assert_equal(expected, image.__lt__(other))
raise SkipTest
def test___repr__(self):
assert_true(repr(self.lena) in [
'Image(mode=RGB shape=(512, 512, 3) type=float64)',
# with some versions
'Image(mode=RGB shape=(512L, 512L, 3L) type=float64)',
]
)
def test_mode(self):
pass # useless ?
def test_open(self):
lena3 = Image.open(path+'/data/lena.png')
assert_equal(self.lena, lena3)
def test_html(self):
pass # do not implement this one as it requires IPython
def test__repr_html_(self):
h = self.lena.convert('P')._repr_html_()
assert_true(h)
def test_average_hash(self):
h1 = self.lena.average_hash()
h2 = self.gray.average_hash()
assert_equal(h1, h2)
def test_perceptual_hash(self):
h1 = self.lena.perceptual_hash()
h2 = self.gray.perceptual_hash()
assert_equal(h1, h2)
def test_base64(self):
# image = Image(data, **kwargs)
# assert_equal(expected, image.base64(fmt))
raise SkipTest # implement your test here
def test_grayscale(self):
pass
def test_invert(self):
# image = Image(data, **kwargs)
# assert_equal(expected, image.invert())
raise SkipTest # implement your test here
def test_ndarray(self):
pass
def test_render(self):
# same as h(self.lena) but without IPython
h = self.lena.render()
assert_true(h)
"""
def test_convert(self):
for mode in modes:
im = self.lena.convert(mode)
assert_image(im, 'convert_%s.png' % mode)
try:
im2 = im.convert('RGB')
assert_image(im2, 'convert_%s_round_trip.png' % mode)
except Exception as e:
logging.error(
'%s round trip conversion failed with %s' % (mode, e))
im2 = im.convert('RGB')
"""
def test_split(self):
rgb = self.lena.split()
for im, c in zip(rgb, 'RGB'):
assert_image(im, 'split_%s.png' % c)
assert_image(Image(rgb), 'RGB_merge.png')
colors = ['Cyan', 'Magenta', 'Yellow', 'blacK']
cmyk = self.lena.split('CMYK')
cmyk2 = [im.colorize(col) for im, col in zip(cmyk, colors)]
for im, c in zip(cmyk2, 'CMYK'):
assert_image(im, 'split_%s.png' % c)
assert_image(Image(cmyk, mode='CMYK'), 'CMYK_merge.png')
lab = self.lena.split('Lab')
for im, c in zip(lab, 'LAB'):
assert_image(im, 'split_%s.png' % c)
lab = Image(lab, mode='LAB')
assert_image(lab, 'Lab.png')
def test_filter(self):
from PIL.ImageFilter import BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
for f in [BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN]:
r = self.lena.filter(f)
assert_image(r, 'filter_pil_%s.png' % f.__name__)
from skimage.filters import sobel, prewitt, scharr, roberts
from skimage.restoration import denoise_bilateral
for f in [sobel, prewitt, scharr, roberts, denoise_bilateral, ]:
try:
assert_image(self.lena.filter(
f), 'filter_sk_%s.png' % f.__name__)
except:
pass
def test_dither(self):
for k in dithering:
im = self.gray.dither(k)*255
assert_image(im, 'dither_%s.png' % dithering[k])
assert_image(self.lena.dither(), 'dither_color_2.png')
assert_image(self.lena.dither(n=4), 'dither_color_4.png')
def test_resize(self):
size = 128
im = self.lena.resize((size, size))
assert_image(im, 'resize_%d.png' % size)
im = self.camera.resize((size, size))
assert_image(im, 'camera_resize_%d.png' % size)
def test_expand(self):
size = 128
im = self.lena.resize((size, size))
# can you see that half pixel border ?
im = im.expand((size+1, size+1), .5, .5)
assert_image(im, 'expand_0.5 border.png')
def test_add(self):
dot = disk(20)
im = Image()
for i in range(3):
for j in range(3):
im = im.add(dot, (j*38.5, i*41.5), 0.5)
assert_image(im, 'image_add.png')
def test_colorize(self):
cmyk = self.lena.split('CMYK')
colors = ['Cyan', 'Magenta', 'Yellow', 'blacK']
cmyk2 = [im.colorize(col) for im, col in zip(cmyk, colors)]
# what a strange syntax ...
back = cmyk2[0]-(-cmyk2[1])-(-cmyk2[2])-(-cmyk2[3])
assert_image(back, 'image_add_sum_cmyk.png')
assert_equal(self.lena.dist(back), 0)
def test_mul(self):
mask = disk(self.lena.size[0]/2)
res = self.lena*mask
assert_image(res, 'disk_mul.png')
def test_shift(self):
left = self.lena[:, 0:256]
right = self.lena[:, 256:]
blank = Image(size=(513, 513), mode='RGB', color='white')
blank.paste(left, (0, 0))
blank.paste(right.shift(1, 1), (256, 0))
assert_image(blank, 'image_stitched.png')
def test___abs__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__abs__())
raise SkipTest # implement your test here
def test___add__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__add__(other))
raise SkipTest # implement your test here
def test___div__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__div__(f))
raise SkipTest # implement your test here
def test___mul__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__mul__(other))
raise SkipTest # implement your test here
def test___nonzero__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__nonzero__())
raise SkipTest # implement your test here
def test___radd__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__radd__(other))
raise SkipTest # implement your test here
def test___sub__(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.__sub__(other))
raise SkipTest # implement your test here
def test_compose(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.compose(other, a, b))
raise SkipTest # implement your test here
def test_correlation(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.correlation(other))
raise SkipTest # implement your test here
def test_crop(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.crop(lurb))
raise SkipTest # implement your test here
def test_getdata(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.getdata(dtype))
raise SkipTest # implement your test here
def test_getpixel(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.getpixel(yx))
raise SkipTest # implement your test here
def test_load(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.load(path))
raise SkipTest # implement your test here
def test_nchannels(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.nchannels())
raise SkipTest # implement your test here
def test_normalize(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.normalize(newmax, newmin))
raise SkipTest # implement your test here
def test_npixels(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.npixels())
raise SkipTest # implement your test here
def test_paste(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.paste(image, box, mask))
raise SkipTest # implement your test here
def test_putpixel(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.putpixel(yx, value))
raise SkipTest # implement your test here
def test_quantize(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.quantize(levels))
raise SkipTest # implement your test here
def test_save(self):
pass # tested everywhere
def test_scale(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.scale(s))
raise SkipTest # implement your test here
def test_shape(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.shape())
raise SkipTest # implement your test here
def test_size(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.size())
raise SkipTest # implement your test here
def test_threshold(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.threshold(level))
raise SkipTest # implement your test here
def test_getcolors(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.getcolors(maxcolors))
raise SkipTest # implement your test here
def test_getpalette(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.getpalette(maxcolors))
raise SkipTest # implement your test here
def test_setpalette(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.setpalette(p))
raise SkipTest # implement your test here
def test_new(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.new(size, color))
raise SkipTest # implement your test here
def test_pil(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.pil())
raise SkipTest # implement your test here
def test_optimize(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.optimize(maxcolors))
raise SkipTest # implement your test here
def test_replace(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.replace(pairs))
raise SkipTest # implement your test here
def test_sub(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.sub(other, pos, alpha, mode))
raise SkipTest # implement your test here
def test_deltaE(self):
# image = Image(data, mode, **kwargs)
# assert_equal(expected, image.deltaE(other))
raise SkipTest # implement your test here
class TestCorrelation:
def test_correlation(self):
# assert_equal(expected, correlation(input, match))
raise SkipTest # implement your test here
class TestAlphaToColor:
def test_alpha_to_color(self):
# assert_equal(expected, alpha_to_color(image, color))
raise SkipTest # implement your test here
class TestAlphaComposite:
def test_alpha_composite(self):
# assert_equal(expected, alpha_composite(front, back))
raise SkipTest # implement your test here
class TestAlphaCompositeWithColor:
def test_alpha_composite_with_color(self):
# assert_equal(expected, alpha_composite_with_color(image, color))
raise SkipTest # implement your test here
class TestPurePilAlphaToColorV1:
def test_pure_pil_alpha_to_color_v1(self):
# assert_equal(expected, pure_pil_alpha_to_color_v1(image, color))
raise SkipTest # implement your test here
class TestPurePilAlphaToColorV2:
def test_pure_pil_alpha_to_color_v2(self):
# assert_equal(expected, pure_pil_alpha_to_color_v2(image, color))
raise SkipTest # implement your test here
class TestNchannels:
def test_nchannels(self):
# assert_equal(expected, nchannels(arr))
raise SkipTest # implement your test here
class TestGuessmode:
def test_guessmode(self):
# assert_equal(expected, guessmode(arr))
raise SkipTest # implement your test here
class TestAdaptRgb:
def test_adapt_rgb(self):
# assert_equal(expected, adapt_rgb(func))
raise SkipTest # implement your test here
class TestRgb2rgba:
def test_rgb2rgba(self):
pass # tested in test_convert
class TestDisk:
def test_disk(self):
# assert_equal(expected, disk(radius, antialias))
raise SkipTest # implement your test here
class TestFspecial:
def test_fspecial(self):
# assert_equal(expected, fspecial(name, **kwargs))
raise SkipTest # implement your test here
class TestReadPdf:
def test_read_pdf(self):
# assert_equal(expected, read_pdf(filename, **kwargs))
raise SkipTest # implement your test here
class TestFig2img:
def test_fig2img(self):
# assert_equal(expected, fig2img(fig))
raise SkipTest # implement your test here
class TestQuantize:
def test_quantize(self):
# assert_equal(expected, quantize(image, N, L))
raise SkipTest # implement your test here
class TestDither:
def test_dither(self):
# assert_equal(expected, dither(image, method, N, L))
raise SkipTest # implement your test here
class TestGray2rgb:
def test_gray2rgb(self):
pass # tested in test_convert
class TestBool2gray:
def test_bool2gray(self):
pass # tested in test_convert
raise SkipTest # implement your test here
class TestRgba2rgb:
def test_rgba2rgb(self):
pass # tested in test_convert
class TestPalette:
def test_palette(self):
# assert_equal(expected, palette(im, ncolors))
raise SkipTest # implement your test here
def test___init__(self):
# palette = Palette(data, n)
raise SkipTest # implement your test here
def test_index(self):
# palette = Palette(data, n)
# assert_equal(expected, palette.index(c, dE))
raise SkipTest # implement your test here
def test_pil(self):
# palette = Palette(data, n)
# assert_equal(expected, palette.pil())
raise SkipTest # implement your test here
def test_update(self):
# palette = Palette(data, n)
# assert_equal(expected, palette.update(data, n))
raise SkipTest # implement your test here
def test___repr__(self):
# palette = Palette(data, n)
# assert_equal(expected, palette.__repr__())
raise SkipTest # implement your test here
def test_patches(self):
# palette = Palette(data, n)
# assert_equal(expected, palette.patches(wide, size))
raise SkipTest # implement your test here
def test_sorted(self):
# palette = Palette(data, n)
# assert_equal(expected, palette.sorted(key))
raise SkipTest # implement your test here
class TestLab2ind:
def test_lab2ind(self):
raise SkipTest # implement your test here
class TestInd2any:
def test_ind2any(self):
# assert_equal(expected, ind2any(im, palette, dest))
raise SkipTest # implement your test here
class TestInd2rgb:
def test_ind2rgb(self):
# assert_equal(expected, ind2rgb(im, palette))
raise SkipTest # implement your test here
class TestRandomize:
def test_randomize(self):
# assert_equal(expected, randomize(image, N, L))
raise SkipTest # implement your test here
class TestDitherer:
def test___call__(self):
# ditherer = Ditherer(name, method)
# assert_equal(expected, ditherer.__call__(image, N))
raise SkipTest # implement your test here
def test___init__(self):
# ditherer = Ditherer(name, method)
raise SkipTest # implement your test here
def test___repr__(self):
# ditherer = Ditherer(name, method)
# assert_equal(expected, ditherer.__repr__())
raise SkipTest # implement your test here
class TestErrorDiffusion:
def test___call__(self):
# error_diffusion = ErrorDiffusion(name, positions, weights)
# assert_equal(expected, error_diffusion.__call__(image, N))
raise SkipTest # implement your test here
def test___init__(self):
# error_diffusion = ErrorDiffusion(name, positions, weights)
raise SkipTest # implement your test here
class TestFloydSteinberg:
def test___call__(self):
# floyd_steinberg = FloydSteinberg()
# assert_equal(expected, floyd_steinberg.__call__(image, N))
raise SkipTest # implement your test here
def test___init__(self):
# floyd_steinberg = FloydSteinberg()
raise SkipTest # implement your test here
class TestNormalize:
def test_normalize(self):
# assert_equal(expected, normalize(a, newmax, newmin))
raise SkipTest # implement your test here
if __name__ == "__main__":
runmodule()