File: test_tiff_image.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 (91 lines) | stat: -rw-r--r-- 2,819 bytes parent folder | download
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
import os
import sys
import atexit
from tempfile import mktemp
from numpy import (uint8, uint16, uint32, uint64, int8, int16, int32,
                   int64, float32, float64, array, zeros, dtype,
                   complex64, complex128)
from libtiff import TIFFfile, TIFFimage, TIFF

import pytest

SUPPORTED_DTYPES = [uint8, uint16, uint32, uint64,
                    int8, int16, int32, int64,
                    float32, float64,
                    complex64, complex128]


@pytest.mark.skipif(sys.platform == "darwin", reason="OSX can't resize mmap")
def test_rw_rgb():
    itype = uint8
    dt = dtype(dict(names=list('rgb'), formats=[itype] * 3))

    image = zeros((2, 3), dtype=dt)
    image['r'][:, 0] = 250
    image['g'][:, 1] = 251
    image['b'][:, 2] = 252

    fn = mktemp('.tif')
    tif = TIFFimage(image)
    tif.write_file(fn, compression='lzw')  # , samples='rgb')
    del tif

    tif = TIFFfile(fn)
    data, names = tif.get_samples()
    tif.close()
    atexit.register(os.remove, fn)
    print(image)
    print(data)

    assert itype == data[0].dtype, repr((itype, data[0].dtype))
    assert (image['r'] == data[0]).all()
    assert (image['g'] == data[1]).all()
    assert (image['b'] == data[2]).all()


@pytest.mark.skipif(sys.platform == "darwin", reason="OSX can't resize mmap")
@pytest.mark.parametrize("itype", SUPPORTED_DTYPES)
@pytest.mark.parametrize("compression", ["none", "lzw"])
def test_write_read(compression, itype):
    image = array([[1, 2, 3], [4, 5, 6]], itype)
    fn = mktemp('.tif')
    tif = TIFFimage(image)
    tif.write_file(fn, compression=compression)
    del tif

    tif = TIFFfile(fn)
    data, names = tif.get_samples()
    tif.close()
    atexit.register(os.remove, fn)
    assert names == ['sample0'], repr(names)
    assert len(data) == 1, repr(len(data))
    assert image.dtype == data[0].dtype, repr(
        (image.dtype, data[0].dtype))
    assert (image == data[0]).all()


@pytest.mark.skipif(sys.platform == "darwin", reason="OSX can't resize mmap")
@pytest.mark.parametrize("itype", SUPPORTED_DTYPES)
def test_write_lzw(itype):
    # image = array([[1,2,3], [4,5,6]], itype)
    image = array([list(range(10000))], itype)
    # image = array([[0]*14000], itype)
    fn = mktemp('.tif')
    tif = TIFFimage(image)
    tif.write_file(fn, compression='lzw')
    del tif

    # os.system('wc %s; echo %s' % (fn, image.nbytes))

    tif = TIFF.open(fn, 'r')
    image2 = tif.read_image()
    tif.close()
    atexit.register(os.remove, fn)
    for i in range(image.size):
        if image.flat[i] != image2.flat[i]:
            print(repr((i, image.flat[i - 5:i + 5].view(dtype=uint8),
                        image2.flat[i - 5:i + 5].view(dtype=uint8))))
            break

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