diff --git a/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/__init__.py b/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/__init__.py index 9f8a84c22..287c25999 100644 --- a/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/__init__.py +++ b/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/__init__.py @@ -1,6 +1,7 @@ """itkwasm-image-io-wasi: Input and output for scientific and medical image file formats. WASI implementation.""" from .read_image import read_image, imread +from .write_image import write_image, imwrite from .bio_rad_read_image import bio_rad_read_image from .bio_rad_write_image import bio_rad_write_image diff --git a/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/write_image.py b/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/write_image.py new file mode 100644 index 000000000..2e2da1e60 --- /dev/null +++ b/packages/image-io/python/itkwasm-image-io-wasi/itkwasm_image_io_wasi/write_image.py @@ -0,0 +1,69 @@ +import os +import importlib +from pathlib import Path +from typing import Optional, Union + +from itkwasm import Image, PixelTypes, IntTypes, FloatTypes, cast_image + +from .extension_to_image_io import extension_to_image_io +from .image_io_index import image_io_index + +def write_image( + image: Image, + serialized_image: os.PathLike, + information_only: bool = False, + use_compression: bool = False, +) -> None: + """Write an itk-wasm Image to an image file format. + + :param image: Input image + :type image: Image + + :param serialized_image: Output image serialized in the file format. + :type serialized_image: str + + :param information_only: Only write image metadata -- do not write pixel data. + :type information_only: bool + + :param use_compression: Use compression in the written file + :type use_compression: bool + + :param serialized_image: Input image serialized in the file format + :type serialized_image: os.PathLike + """ + extension = ''.join(Path(serialized_image).suffixes) + + io = None + if extension in extension_to_image_io: + func = f"{extension_to_image_io[extension]}_write_image" + mod_name = f"itkwasm_image_io_wasi.{func}" + mod = importlib.import_module(mod_name) + io = getattr(mod, func) + else: + for ioname in image_io_index: + func = f"{ioname}_write_image" + mod_name = f"itkwasm_image_io_wasi.{func}" + mod = importlib.import_module(mod_name) + io = getattr(mod, func) + could_write = io(image, serialized_image, information_only=information_only, use_compression=use_compression) + if could_write: + return + + if io is None: + raise RuntimeError(f"Could not find an image writer for {extension}") + + could_write = io(image, serialized_image, information_only=information_only, use_compression=use_compression) + if not could_write: + raise RuntimeError(f"Could not write {serialized_image}") + +def imwrite( + image: Image, + serialized_image: os.PathLike, + information_only: bool = False, + use_compression: bool = False, +) -> None: + return write_image(image, serialized_image, information_only=information_only, use_compression=use_compression) + +imwrite.__doc__ = f"""{write_image.__doc__} + Alias for write_image. + """ \ No newline at end of file diff --git a/packages/image-io/python/itkwasm-image-io-wasi/tests/test_read_write_image.py b/packages/image-io/python/itkwasm-image-io-wasi/tests/test_read_write_image.py index 38c882774..2b50a991c 100644 --- a/packages/image-io/python/itkwasm-image-io-wasi/tests/test_read_write_image.py +++ b/packages/image-io/python/itkwasm-image-io-wasi/tests/test_read_write_image.py @@ -2,7 +2,7 @@ from itkwasm import PixelTypes, IntTypes -from itkwasm_image_io_wasi import read_image, imread +from itkwasm_image_io_wasi import read_image, imread, write_image, imwrite from .common import test_input_path, test_output_path @@ -44,15 +44,19 @@ def test_imread(): verify_image(image) def test_write_image(): - pass - return - could_read, image = png_read_image(test_input_file_path) - assert could_read + image = read_image(test_input_file_path) + + use_compression = False + write_image(image, test_output_file_path, use_compression=use_compression) + + image = read_image(test_output_file_path) + verify_image(image) + +def test_imwrite(): + image = imread(test_input_file_path) use_compression = False - could_write = png_write_image(image, test_output_file_path, use_compression) - assert could_write + imwrite(image, test_output_file_path, use_compression=use_compression) - could_read, image = png_read_image(test_output_file_path) - assert could_read + image = imread(test_output_file_path) verify_image(image)