File: test_simpleitk.py

package info (click to toggle)
python-imageio 2.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,016 kB
  • sloc: python: 26,044; makefile: 138
file content (51 lines) | stat: -rw-r--r-- 1,196 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
""" Test simpleitk plugin functionality.
"""

import numpy as np

import pytest
from pytest import raises

import imageio.v2 as iio

try:
    import itk  # type: ignore
except ImportError:
    try:
        import SimpleITK as itk  # type: ignore
    except ImportError:
        itk = None


@pytest.mark.skipif("itk is None")
def test_simpleitk_reading_writing(tmp_path):
    """Test reading and saveing tiff"""
    im2 = np.ones((10, 10, 3), np.uint8) * 2

    filename1 = tmp_path / "test_tiff.tiff"

    # One image
    iio.imsave(filename1, im2, "itk")
    im = iio.imread(filename1, "itk")
    print(im.shape)
    ims = iio.mimread(filename1, "itk")
    print(im2.shape)
    assert (im == im2).all()
    assert len(ims) == 1

    # Mixed
    W = iio.save(filename1, "itk")
    raises(RuntimeError, W.set_meta_data, 1)
    assert W.format.name == "ITK"
    W.append_data(im2)
    W.append_data(im2)
    W.close()
    #
    R = iio.read(filename1, "itk")
    assert R.format.name == "ITK"
    ims = list(R)  # == [im for im in R]
    assert (ims[0] == im2).all()
    # Fail
    raises(IndexError, R.get_data, -1)
    raises(IndexError, R.get_data, 3)
    raises(RuntimeError, R.get_meta_data)