File: test_simple.py

package info (click to toggle)
pylibtiff 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 976 kB
  • sloc: python: 10,219; ansic: 1,064; makefile: 8
file content (47 lines) | stat: -rw-r--r-- 1,299 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
import os
from tempfile import mktemp
from numpy import (uint8, uint16, uint32, uint64, int8, int16, int32,
                   int64, float32, float64, complex64, complex128,
                   array, random)
from libtiff import TIFF


def test_write_read():
    for itype in [uint8, uint16, uint32, uint64,
                  int8, int16, int32, int64,
                  float32, float64,
                  complex64, complex128]:
        image = array([[1, 2, 3], [4, 5, 6]], itype)
        fn = mktemp('.tif')
        tif = TIFF.open(fn, 'w')
        tif.write_image(image)
        tif.close()

        tif = TIFF.open(fn, 'r')
        image2 = tif.read_image()
        tif.close()
        os.remove(fn)

        assert image.dtype == image2.dtype
        assert (image == image2).all()


def test_slicing():
    shape = (16, 16)
    image = random.randint(255, size=shape)

    for i in range(shape[0]):
        for j in range(shape[1]):
            image1 = image[:i + 1, :j + 1]
            fn = mktemp('.tif')
            tif = TIFF.open(fn, 'w')
            tif.write_image(image1)
            tif.close()

            tif = TIFF.open(fn, 'r')
            image2 = tif.read_image()
            tif.close()

            assert (image1 == image2).all(), repr((i, j))

            os.remove(fn)