Test basic functionality of ImageRead and ImageWrite procedures
(test_dir)
| 76 | |
| 77 | |
| 78 | def test_write_procedure(test_dir): |
| 79 | """Test basic functionality of ImageRead and ImageWrite procedures""" |
| 80 | # Test single slice image with compression |
| 81 | img = sitk.Image([32, 32], sitk.sitkUInt8) |
| 82 | output_path = test_dir / "test_write_1.tiff" |
| 83 | sitk.WriteImage( |
| 84 | img, |
| 85 | output_path, |
| 86 | useCompression=True, |
| 87 | compressor="DEFLATE", |
| 88 | compressionLevel=1 |
| 89 | ) |
| 90 | rimg = sitk.ReadImage(output_path) |
| 91 | assert sitk.Hash(img) == sitk.Hash(rimg), "Image hash mismatch after TIFF compression" |
| 92 | |
| 93 | # Test JPEG write/read |
| 94 | output_path = test_dir / "test_write_2.jpg" |
| 95 | sitk.WriteImage(img, output_path, True, 1) |
| 96 | rimg = sitk.ReadImage(output_path, imageIO="JPEGImageIO") |
| 97 | # Note: Don't check hash for JPEG as it's lossy compression |
| 98 | |
| 99 | # Test image series write/read |
| 100 | img = sitk.Image([32, 32, 4], sitk.sitkUInt8) |
| 101 | series_paths = [test_dir / f"series_{i}.tif" for i in range(img.GetSize()[2])] |
| 102 | sitk.WriteImage(img, series_paths, compressionLevel=90) |
| 103 | read_img = sitk.ReadImage(series_paths, imageIO="TIFFImageIO") |
| 104 | assert img.GetSize() == read_img.GetSize(), "Image size changed after series write/read" |
| 105 | |
| 106 | with pytest.raises(RuntimeError): |
| 107 | sitk.WriteImage(img, "this.isafilenamewithnoimageio") |
| 108 | |
| 109 | |
| 110 |