File: test_create.py

package info (click to toggle)
vips 8.18.0-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 53,448 kB
  • sloc: ansic: 172,621; cpp: 12,257; python: 5,077; sh: 773; perl: 40; makefile: 25; javascript: 6
file content (499 lines) | stat: -rw-r--r-- 16,482 bytes parent folder | download | duplicates (2)
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
# vim: set fileencoding=utf-8 :
import pytest

import pyvips
from helpers import *


class TestCreate:
    def test_black(self):
        im = pyvips.Image.black(100, 100)

        assert im.width == 100
        assert im.height == 100
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.bands == 1
        for i in range(0, 100):
            pixel = im(i, i)
            assert len(pixel) == 1
            assert pixel[0] == 0

        im = pyvips.Image.black(100, 100, bands=3)

        assert im.width == 100
        assert im.height == 100
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.bands == 3
        for i in range(0, 100):
            pixel = im(i, i)
            assert len(pixel) == 3
            assert_almost_equal_objects(pixel, [0, 0, 0])

    def test_buildlut(self):
        M = pyvips.Image.new_from_array([[0, 0],
                                         [255, 100]])
        lut = M.buildlut()
        assert lut.width == 256
        assert lut.height == 1
        assert lut.bands == 1
        p = lut(0, 0)
        assert p[0] == 0.0
        p = lut(255, 0)
        assert p[0] == 100.0
        p = lut(10, 0)
        assert p[0] == 100 * 10.0 / 255.0

        M = pyvips.Image.new_from_array([[0, 0, 100],
                                         [255, 100, 0],
                                         [128, 10, 90]])
        lut = M.buildlut()
        assert lut.width == 256
        assert lut.height == 1
        assert lut.bands == 2
        p = lut(0, 0)
        assert_almost_equal_objects(p, [0.0, 100.0])
        p = lut(64, 0)
        assert_almost_equal_objects(p, [5.0, 95.0])

    def test_eye(self):
        im = pyvips.Image.eye(100, 90)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.max() == 1.0
        assert im.min() == -1.0

        im = pyvips.Image.eye(100, 90, uchar=True)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.max() == 255.0
        assert im.min() == 0.0

    @skip_if_no("fwfft")
    def test_fractsurf(self):
        im = pyvips.Image.fractsurf(100, 90, 2.5)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

    def test_gaussmat(self):
        im = pyvips.Image.gaussmat(1, 0.1)
        assert im.width == 5
        assert im.height == 5
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.DOUBLE
        assert im.max() == 20
        total = im.avg() * im.width * im.height
        scale = im.get("scale")
        assert total == scale
        p = im(im.width / 2, im.height / 2)
        assert p[0] == 20.0

        im = pyvips.Image.gaussmat(1, 0.1,
                                   separable=True, precision="float")
        assert im.width == 5
        assert im.height == 1
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.DOUBLE
        assert im.max() == 1.0
        total = im.avg() * im.width * im.height
        scale = im.get("scale")
        assert total == scale
        p = im(im.width / 2, im.height / 2)
        assert p[0] == 1.0

    def test_gaussnoise(self):
        im = pyvips.Image.gaussnoise(100, 90)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

        im = pyvips.Image.gaussnoise(100, 90, sigma=10, mean=100)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

        sigma = im.deviate()
        mean = im.avg()

        assert sigma == pytest.approx(10, abs=0.4)
        assert mean == pytest.approx(100, abs=0.4)

    def test_grey(self):
        im = pyvips.Image.grey(100, 90)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

        p = im(0, 0)
        assert p[0] == 0.0
        p = im(99, 0)
        assert p[0] == 1.0
        p = im(0, 89)
        assert p[0] == 0.0
        p = im(99, 89)
        assert p[0] == 1.0

        im = pyvips.Image.grey(100, 90, uchar=True)
        assert im.width == 100
        assert im.height == 90
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR

        p = im(0, 0)
        assert p[0] == 0
        p = im(99, 0)
        assert p[0] == 255
        p = im(0, 89)
        assert p[0] == 0
        p = im(99, 89)
        assert p[0] == 255

    def test_identity(self):
        im = pyvips.Image.identity()
        assert im.width == 256
        assert im.height == 1
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR

        p = im(0, 0)
        assert p[0] == 0.0
        p = im(255, 0)
        assert p[0] == 255.0
        p = im(128, 0)
        assert p[0] == 128.0

        im = pyvips.Image.identity(ushort=True)
        assert im.width == 65536
        assert im.height == 1
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.USHORT

        p = im(0, 0)
        assert p[0] == 0
        p = im(99, 0)
        assert p[0] == 99
        p = im(65535, 0)
        assert p[0] == 65535

    def test_invertlut(self):
        lut = pyvips.Image.new_from_array([[0.1, 0.2, 0.3, 0.1],
                                           [0.2, 0.4, 0.4, 0.2],
                                           [0.7, 0.5, 0.6, 0.3]])
        im = lut.invertlut()
        assert im.width == 256
        assert im.height == 1
        assert im.bands == 3
        assert im.format == pyvips.BandFormat.DOUBLE

        p = im(0, 0)
        assert_almost_equal_objects(p, [0, 0, 0])
        p = im(255, 0)
        assert_almost_equal_objects(p, [1, 1, 1])
        p = im(0.2 * 255, 0)
        assert p[0] == pytest.approx(0.1, abs=0.1)
        p = im(0.3 * 255, 0)
        assert p[1] == pytest.approx(0.1, abs=0.1)
        p = im(0.1 * 255, 0)
        assert p[2] == pytest.approx(0.1, abs=0.1)

    def test_matrixinvert(self):
        # 4x4 matrix to check if PLU decomposition works
        mat = pyvips.Image.new_from_array([[4, 0, 0, 0],
                                           [0, 0, 2, 0],
                                           [0, 1, 2, 0],
                                           [1, 0, 0, 1]])
        im = mat.matrixinvert()
        assert im.width == 4
        assert im.height == 4
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.DOUBLE

        p = im(0, 0)
        assert p[0] == 0.25
        p = im(3, 3)
        assert p[0] == 1.0

    def test_logmat(self):
        im = pyvips.Image.logmat(1, 0.1)
        assert im.width == 7
        assert im.height == 7
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.DOUBLE
        assert im.max() == 20
        total = im.avg() * im.width * im.height
        scale = im.get("scale")
        assert total == scale
        p = im(im.width / 2, im.height / 2)
        assert p[0] == 20.0

        im = pyvips.Image.logmat(1, 0.1,
                                 separable=True, precision="float")
        assert im.width == 7
        assert im.height == 1
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.DOUBLE
        assert im.max() == 1.0
        total = im.avg() * im.width * im.height
        scale = im.get("scale")
        assert total == scale
        p = im(im.width / 2, im.height / 2)
        assert p[0] == 1.0

    def test_mask_butterworth_band(self):
        im = pyvips.Image.mask_butterworth_band(128, 128, 2,
                                                0.5, 0.5, 0.7,
                                                0.1)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.max() == pytest.approx(1, abs=0.01)
        p = im(32, 32)
        assert p[0] == 1.0

        im = pyvips.Image.mask_butterworth_band(128, 128, 2,
                                                0.5, 0.5, 0.7,
                                                0.1, uchar=True, optical=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.max() == 255
        p = im(32, 32)
        assert p[0] == 255.0
        p = im(64, 64)
        assert p[0] == 255.0

        im = pyvips.Image.mask_butterworth_band(128, 128, 2,
                                                0.5, 0.5, 0.7,
                                                0.1, uchar=True, optical=True,
                                                nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.max() == 255
        p = im(32, 32)
        assert p[0] == 255.0
        p = im(64, 64)
        assert p[0] != 255

    def test_mask_butterworth(self):
        im = pyvips.Image.mask_butterworth(128, 128, 2, 0.7, 0.1,
                                           nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.min() == pytest.approx(0, abs=0.01)
        p = im(0, 0)
        assert p[0] == 0.0
        v, x, y = im.maxpos()
        assert x == 64
        assert y == 64

        im = pyvips.Image.mask_butterworth(128, 128, 2, 0.7, 0.1,
                                           optical=True, uchar=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.min() == pytest.approx(0, abs=0.01)
        p = im(64, 64)
        assert p[0] == 255

    def test_mask_butterworth_ring(self):
        im = pyvips.Image.mask_butterworth_ring(128, 128, 2, 0.7, 0.1, 0.5,
                                                nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        p = im(45, 0)
        assert p[0] == pytest.approx(1.0, abs=0.0001)
        v, x, y = im.minpos()
        assert x == 64
        assert y == 64

    def test_mask_fractal(self):
        im = pyvips.Image.mask_fractal(128, 128, 2.3)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

    def test_mask_gaussian_band(self):
        im = pyvips.Image.mask_gaussian_band(128, 128, 0.5, 0.5, 0.7, 0.1)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.max() == pytest.approx(1, abs=0.01)
        p = im(32, 32)
        assert p[0] == 1.0

    def test_mask_gaussian(self):
        im = pyvips.Image.mask_gaussian(128, 128, 0.7, 0.1,
                                        nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.min() == pytest.approx(0, abs=0.01)
        p = im(0, 0)
        assert p[0] == 0.0

    def test_mask_gaussian_ring(self):
        im = pyvips.Image.mask_gaussian_ring(128, 128, 0.7, 0.1, 0.5,
                                             nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        p = im(45, 0)
        assert p[0] == pytest.approx(1.0, abs=0.001)

    def test_mask_ideal_band(self):
        im = pyvips.Image.mask_ideal_band(128, 128, 0.5, 0.5, 0.7)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.max() == pytest.approx(1, abs=0.01)
        p = im(32, 32)
        assert p[0] == 1.0

    def test_mask_ideal(self):
        im = pyvips.Image.mask_ideal(128, 128, 0.7,
                                     nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.min() == pytest.approx(0, abs=0.01)
        p = im(0, 0)
        assert p[0] == 0.0

    def test_mask_gaussian_ring_2(self):
        im = pyvips.Image.mask_ideal_ring(128, 128, 0.7, 0.5,
                                          nodc=True)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        p = im(45, 0)
        assert p[0] == pytest.approx(1, abs=0.001)

    def test_sines(self):
        im = pyvips.Image.sines(128, 128)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

    @skip_if_no("text")
    def test_text(self):
        im = pyvips.Image.text("Hello, world!", dpi=300)
        assert im.width > 10
        assert im.height > 10
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.UCHAR
        assert im.max() > 240
        assert im.min() == 0

        # test autofit
        im = pyvips.Image.text("Hello, world!", width=500, height=500)
        # quite a large threshold, since we need to work with a huge range of
        # text rendering systems
        assert abs(im.width - 500) < 50

        # test wrap
        im1 = pyvips.Image.text("helloworld", width=100, dpi=500)
        im2 = pyvips.Image.text("helloworld", width=100, dpi=500, wrap="char")
        assert im1.width > im2.width

    def test_tonelut(self):
        im = pyvips.Image.tonelut()
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.USHORT
        assert im.width == 32768
        assert im.height == 1
        assert im.hist_ismonotonic()

    def test_xyz(self):
        im = pyvips.Image.xyz(128, 128)
        assert im.bands == 2
        assert im.format == pyvips.BandFormat.UINT
        assert im.width == 128
        assert im.height == 128
        p = im(45, 35)
        assert_almost_equal_objects(p, [45, 35])

    def test_sdf(self):
        im = pyvips.Image.sdf(128, 128, "circle", a=[64, 64], r=32)
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.width == 128
        assert im.height == 128
        p = im(45, 35)
        assert_almost_equal_objects(p, [2.670], threshold=0.01)

        im = pyvips.Image.sdf(128, 128, "box", a=[10, 10], b=[50, 40])
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.width == 128
        assert im.height == 128
        p = im(45, 35)
        assert_almost_equal_objects(p, [-5.0])

        im = pyvips.Image.sdf(128, 128, "rounded-box",
                              a=[10, 10],
                              b=[50, 40],
                              corners=[50, 0, 0, 0])
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.width == 128
        assert im.height == 128
        p = im(45, 35)
        assert_almost_equal_objects(p, [13.640], threshold=0.01)

        im = pyvips.Image.sdf(128, 128, "line", a=[10, 10], b=[50, 40])
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT
        assert im.width == 128
        assert im.height == 128
        p = im(45, 35)
        assert_almost_equal_objects(p, [1.0])

    def test_zone(self):
        im = pyvips.Image.zone(128, 128)
        assert im.width == 128
        assert im.height == 128
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

    def test_worley(self):
        im = pyvips.Image.worley(512, 512)
        assert im.width == 512
        assert im.height == 512
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT

    def test_perlin(self):
        im = pyvips.Image.perlin(512, 512)
        assert im.width == 512
        assert im.height == 512
        assert im.bands == 1
        assert im.format == pyvips.BandFormat.FLOAT


if __name__ == '__main__':
    pytest.main()