1# vim: set fileencoding=utf-8 : 2import pytest 3 4import pyvips 5from helpers import assert_almost_equal_objects 6 7 8class TestCreate: 9 def test_black(self): 10 im = pyvips.Image.black(100, 100) 11 12 assert im.width == 100 13 assert im.height == 100 14 assert im.format == pyvips.BandFormat.UCHAR 15 assert im.bands == 1 16 for i in range(0, 100): 17 pixel = im(i, i) 18 assert len(pixel) == 1 19 assert pixel[0] == 0 20 21 im = pyvips.Image.black(100, 100, bands=3) 22 23 assert im.width == 100 24 assert im.height == 100 25 assert im.format == pyvips.BandFormat.UCHAR 26 assert im.bands == 3 27 for i in range(0, 100): 28 pixel = im(i, i) 29 assert len(pixel) == 3 30 assert_almost_equal_objects(pixel, [0, 0, 0]) 31 32 def test_buildlut(self): 33 M = pyvips.Image.new_from_array([[0, 0], 34 [255, 100]]) 35 lut = M.buildlut() 36 assert lut.width == 256 37 assert lut.height == 1 38 assert lut.bands == 1 39 p = lut(0, 0) 40 assert p[0] == 0.0 41 p = lut(255, 0) 42 assert p[0] == 100.0 43 p = lut(10, 0) 44 assert p[0] == 100 * 10.0 / 255.0 45 46 M = pyvips.Image.new_from_array([[0, 0, 100], 47 [255, 100, 0], 48 [128, 10, 90]]) 49 lut = M.buildlut() 50 assert lut.width == 256 51 assert lut.height == 1 52 assert lut.bands == 2 53 p = lut(0, 0) 54 assert_almost_equal_objects(p, [0.0, 100.0]) 55 p = lut(64, 0) 56 assert_almost_equal_objects(p, [5.0, 95.0]) 57 58 def test_eye(self): 59 im = pyvips.Image.eye(100, 90) 60 assert im.width == 100 61 assert im.height == 90 62 assert im.bands == 1 63 assert im.format == pyvips.BandFormat.FLOAT 64 assert im.max() == 1.0 65 assert im.min() == -1.0 66 67 im = pyvips.Image.eye(100, 90, uchar=True) 68 assert im.width == 100 69 assert im.height == 90 70 assert im.bands == 1 71 assert im.format == pyvips.BandFormat.UCHAR 72 assert im.max() == 255.0 73 assert im.min() == 0.0 74 75 @pytest.mark.skipif(pyvips.type_find("VipsOperation", "fwfft") == 0, 76 reason="no FFTW, skipping test") 77 def test_fractsurf(self): 78 im = pyvips.Image.fractsurf(100, 90, 2.5) 79 assert im.width == 100 80 assert im.height == 90 81 assert im.bands == 1 82 assert im.format == pyvips.BandFormat.FLOAT 83 84 def test_gaussmat(self): 85 im = pyvips.Image.gaussmat(1, 0.1) 86 assert im.width == 5 87 assert im.height == 5 88 assert im.bands == 1 89 assert im.format == pyvips.BandFormat.DOUBLE 90 assert im.max() == 20 91 total = im.avg() * im.width * im.height 92 scale = im.get("scale") 93 assert total == scale 94 p = im(im.width / 2, im.height / 2) 95 assert p[0] == 20.0 96 97 im = pyvips.Image.gaussmat(1, 0.1, 98 separable=True, precision="float") 99 assert im.width == 5 100 assert im.height == 1 101 assert im.bands == 1 102 assert im.format == pyvips.BandFormat.DOUBLE 103 assert im.max() == 1.0 104 total = im.avg() * im.width * im.height 105 scale = im.get("scale") 106 assert total == scale 107 p = im(im.width / 2, im.height / 2) 108 assert p[0] == 1.0 109 110 def test_gaussnoise(self): 111 im = pyvips.Image.gaussnoise(100, 90) 112 assert im.width == 100 113 assert im.height == 90 114 assert im.bands == 1 115 assert im.format == pyvips.BandFormat.FLOAT 116 117 im = pyvips.Image.gaussnoise(100, 90, sigma=10, mean=100) 118 assert im.width == 100 119 assert im.height == 90 120 assert im.bands == 1 121 assert im.format == pyvips.BandFormat.FLOAT 122 123 sigma = im.deviate() 124 mean = im.avg() 125 126 assert sigma == pytest.approx(10, abs=0.4) 127 assert mean == pytest.approx(100, abs=0.4) 128 129 def test_grey(self): 130 im = pyvips.Image.grey(100, 90) 131 assert im.width == 100 132 assert im.height == 90 133 assert im.bands == 1 134 assert im.format == pyvips.BandFormat.FLOAT 135 136 p = im(0, 0) 137 assert p[0] == 0.0 138 p = im(99, 0) 139 assert p[0] == 1.0 140 p = im(0, 89) 141 assert p[0] == 0.0 142 p = im(99, 89) 143 assert p[0] == 1.0 144 145 im = pyvips.Image.grey(100, 90, uchar=True) 146 assert im.width == 100 147 assert im.height == 90 148 assert im.bands == 1 149 assert im.format == pyvips.BandFormat.UCHAR 150 151 p = im(0, 0) 152 assert p[0] == 0 153 p = im(99, 0) 154 assert p[0] == 255 155 p = im(0, 89) 156 assert p[0] == 0 157 p = im(99, 89) 158 assert p[0] == 255 159 160 def test_identity(self): 161 im = pyvips.Image.identity() 162 assert im.width == 256 163 assert im.height == 1 164 assert im.bands == 1 165 assert im.format == pyvips.BandFormat.UCHAR 166 167 p = im(0, 0) 168 assert p[0] == 0.0 169 p = im(255, 0) 170 assert p[0] == 255.0 171 p = im(128, 0) 172 assert p[0] == 128.0 173 174 im = pyvips.Image.identity(ushort=True) 175 assert im.width == 65536 176 assert im.height == 1 177 assert im.bands == 1 178 assert im.format == pyvips.BandFormat.USHORT 179 180 p = im(0, 0) 181 assert p[0] == 0 182 p = im(99, 0) 183 assert p[0] == 99 184 p = im(65535, 0) 185 assert p[0] == 65535 186 187 def test_invertlut(self): 188 lut = pyvips.Image.new_from_array([[0.1, 0.2, 0.3, 0.1], 189 [0.2, 0.4, 0.4, 0.2], 190 [0.7, 0.5, 0.6, 0.3]]) 191 im = lut.invertlut() 192 assert im.width == 256 193 assert im.height == 1 194 assert im.bands == 3 195 assert im.format == pyvips.BandFormat.DOUBLE 196 197 p = im(0, 0) 198 assert_almost_equal_objects(p, [0, 0, 0]) 199 p = im(255, 0) 200 assert_almost_equal_objects(p, [1, 1, 1]) 201 p = im(0.2 * 255, 0) 202 assert p[0] == pytest.approx(0.1, abs=0.1) 203 p = im(0.3 * 255, 0) 204 assert p[1] == pytest.approx(0.1, abs=0.1) 205 p = im(0.1 * 255, 0) 206 assert p[2] == pytest.approx(0.1, abs=0.1) 207 208 def test_matrixinvert(self): 209 # 4x4 matrix to check if PLU decomposition works 210 mat = pyvips.Image.new_from_array([[4, 0, 0, 0], 211 [0, 0, 2, 0], 212 [0, 1, 2, 0], 213 [1, 0, 0, 1]]) 214 im = mat.matrixinvert() 215 assert im.width == 4 216 assert im.height == 4 217 assert im.bands == 1 218 assert im.format == pyvips.BandFormat.DOUBLE 219 220 p = im(0, 0) 221 assert p[0] == 0.25 222 p = im(3, 3) 223 assert p[0] == 1.0 224 225 def test_logmat(self): 226 im = pyvips.Image.logmat(1, 0.1) 227 assert im.width == 7 228 assert im.height == 7 229 assert im.bands == 1 230 assert im.format == pyvips.BandFormat.DOUBLE 231 assert im.max() == 20 232 total = im.avg() * im.width * im.height 233 scale = im.get("scale") 234 assert total == scale 235 p = im(im.width / 2, im.height / 2) 236 assert p[0] == 20.0 237 238 im = pyvips.Image.logmat(1, 0.1, 239 separable=True, precision="float") 240 assert im.width == 7 241 assert im.height == 1 242 assert im.bands == 1 243 assert im.format == pyvips.BandFormat.DOUBLE 244 assert im.max() == 1.0 245 total = im.avg() * im.width * im.height 246 scale = im.get("scale") 247 assert total == scale 248 p = im(im.width / 2, im.height / 2) 249 assert p[0] == 1.0 250 251 def test_mask_butterworth_band(self): 252 im = pyvips.Image.mask_butterworth_band(128, 128, 2, 253 0.5, 0.5, 0.7, 254 0.1) 255 assert im.width == 128 256 assert im.height == 128 257 assert im.bands == 1 258 assert im.format == pyvips.BandFormat.FLOAT 259 assert im.max() == pytest.approx(1, abs=0.01) 260 p = im(32, 32) 261 assert p[0] == 1.0 262 263 im = pyvips.Image.mask_butterworth_band(128, 128, 2, 264 0.5, 0.5, 0.7, 265 0.1, uchar=True, optical=True) 266 assert im.width == 128 267 assert im.height == 128 268 assert im.bands == 1 269 assert im.format == pyvips.BandFormat.UCHAR 270 assert im.max() == 255 271 p = im(32, 32) 272 assert p[0] == 255.0 273 p = im(64, 64) 274 assert p[0] == 255.0 275 276 im = pyvips.Image.mask_butterworth_band(128, 128, 2, 277 0.5, 0.5, 0.7, 278 0.1, uchar=True, optical=True, 279 nodc=True) 280 assert im.width == 128 281 assert im.height == 128 282 assert im.bands == 1 283 assert im.format == pyvips.BandFormat.UCHAR 284 assert im.max() == 255 285 p = im(32, 32) 286 assert p[0] == 255.0 287 p = im(64, 64) 288 assert p[0] != 255 289 290 def test_mask_butterworth(self): 291 im = pyvips.Image.mask_butterworth(128, 128, 2, 0.7, 0.1, 292 nodc=True) 293 assert im.width == 128 294 assert im.height == 128 295 assert im.bands == 1 296 assert im.format == pyvips.BandFormat.FLOAT 297 assert im.min() == pytest.approx(0, abs=0.01) 298 p = im(0, 0) 299 assert p[0] == 0.0 300 v, x, y = im.maxpos() 301 assert x == 64 302 assert y == 64 303 304 im = pyvips.Image.mask_butterworth(128, 128, 2, 0.7, 0.1, 305 optical=True, uchar=True) 306 assert im.width == 128 307 assert im.height == 128 308 assert im.bands == 1 309 assert im.format == pyvips.BandFormat.UCHAR 310 assert im.min() == pytest.approx(0, abs=0.01) 311 p = im(64, 64) 312 assert p[0] == 255 313 314 def test_mask_butterworth_ring(self): 315 im = pyvips.Image.mask_butterworth_ring(128, 128, 2, 0.7, 0.1, 0.5, 316 nodc=True) 317 assert im.width == 128 318 assert im.height == 128 319 assert im.bands == 1 320 assert im.format == pyvips.BandFormat.FLOAT 321 p = im(45, 0) 322 assert p[0] == pytest.approx(1.0, abs=0.0001) 323 v, x, y = im.minpos() 324 assert x == 64 325 assert y == 64 326 327 def test_mask_fractal(self): 328 im = pyvips.Image.mask_fractal(128, 128, 2.3) 329 assert im.width == 128 330 assert im.height == 128 331 assert im.bands == 1 332 assert im.format == pyvips.BandFormat.FLOAT 333 334 def test_mask_gaussian_band(self): 335 im = pyvips.Image.mask_gaussian_band(128, 128, 0.5, 0.5, 0.7, 0.1) 336 assert im.width == 128 337 assert im.height == 128 338 assert im.bands == 1 339 assert im.format == pyvips.BandFormat.FLOAT 340 assert im.max() == pytest.approx(1, abs=0.01) 341 p = im(32, 32) 342 assert p[0] == 1.0 343 344 def test_mask_gaussian(self): 345 im = pyvips.Image.mask_gaussian(128, 128, 0.7, 0.1, 346 nodc=True) 347 assert im.width == 128 348 assert im.height == 128 349 assert im.bands == 1 350 assert im.format == pyvips.BandFormat.FLOAT 351 assert im.min() == pytest.approx(0, abs=0.01) 352 p = im(0, 0) 353 assert p[0] == 0.0 354 355 def test_mask_gaussian_ring(self): 356 im = pyvips.Image.mask_gaussian_ring(128, 128, 0.7, 0.1, 0.5, 357 nodc=True) 358 assert im.width == 128 359 assert im.height == 128 360 assert im.bands == 1 361 assert im.format == pyvips.BandFormat.FLOAT 362 p = im(45, 0) 363 assert p[0] == pytest.approx(1.0, abs=0.001) 364 365 def test_mask_ideal_band(self): 366 im = pyvips.Image.mask_ideal_band(128, 128, 0.5, 0.5, 0.7) 367 assert im.width == 128 368 assert im.height == 128 369 assert im.bands == 1 370 assert im.format == pyvips.BandFormat.FLOAT 371 assert im.max() == pytest.approx(1, abs=0.01) 372 p = im(32, 32) 373 assert p[0] == 1.0 374 375 def test_mask_ideal(self): 376 im = pyvips.Image.mask_ideal(128, 128, 0.7, 377 nodc=True) 378 assert im.width == 128 379 assert im.height == 128 380 assert im.bands == 1 381 assert im.format == pyvips.BandFormat.FLOAT 382 assert im.min() == pytest.approx(0, abs=0.01) 383 p = im(0, 0) 384 assert p[0] == 0.0 385 386 def test_mask_gaussian_ring_2(self): 387 im = pyvips.Image.mask_ideal_ring(128, 128, 0.7, 0.5, 388 nodc=True) 389 assert im.width == 128 390 assert im.height == 128 391 assert im.bands == 1 392 assert im.format == pyvips.BandFormat.FLOAT 393 p = im(45, 0) 394 assert p[0] == pytest.approx(1, abs=0.001) 395 396 def test_sines(self): 397 im = pyvips.Image.sines(128, 128) 398 assert im.width == 128 399 assert im.height == 128 400 assert im.bands == 1 401 assert im.format == pyvips.BandFormat.FLOAT 402 403 @pytest.mark.skipif(pyvips.type_find("VipsOperation", "text") == 0, 404 reason="no text, skipping test") 405 def test_text(self): 406 im = pyvips.Image.text("Hello, world!") 407 assert im.width > 10 408 assert im.height > 10 409 assert im.bands == 1 410 assert im.format == pyvips.BandFormat.UCHAR 411 assert im.max() == 255 412 assert im.min() == 0 413 414 # test autofit 415 im = pyvips.Image.text("Hello, world!", width=500, height=500) 416 # quite a large threshold, since we need to work with a huge range of 417 # text rendering systems 418 assert abs(im.width - 500) < 50 419 420 def test_tonelut(self): 421 im = pyvips.Image.tonelut() 422 assert im.bands == 1 423 assert im.format == pyvips.BandFormat.USHORT 424 assert im.width == 32768 425 assert im.height == 1 426 assert im.hist_ismonotonic() 427 428 def test_xyz(self): 429 im = pyvips.Image.xyz(128, 128) 430 assert im.bands == 2 431 assert im.format == pyvips.BandFormat.UINT 432 assert im.width == 128 433 assert im.height == 128 434 p = im(45, 35) 435 assert_almost_equal_objects(p, [45, 35]) 436 437 def test_zone(self): 438 im = pyvips.Image.zone(128, 128) 439 assert im.width == 128 440 assert im.height == 128 441 assert im.bands == 1 442 assert im.format == pyvips.BandFormat.FLOAT 443 444 @pytest.mark.skipif(pyvips.type_find("VipsOperation", "worley") == 0, 445 reason="no worley, skipping test") 446 def test_worley(self): 447 im = pyvips.Image.worley(512, 512) 448 assert im.width == 512 449 assert im.height == 512 450 assert im.bands == 1 451 assert im.format == pyvips.BandFormat.FLOAT 452 453 @pytest.mark.skipif(pyvips.type_find("VipsOperation", "perlin") == 0, 454 reason="no perlin, skipping test") 455 def test_perlin(self): 456 im = pyvips.Image.perlin(512, 512) 457 assert im.width == 512 458 assert im.height == 512 459 assert im.bands == 1 460 assert im.format == pyvips.BandFormat.FLOAT 461 462 463if __name__ == '__main__': 464 pytest.main() 465