Skip to content

Commit

Permalink
feat(itkwasm-image-io-wasi): add write_image
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 4, 2023
1 parent 871ab83 commit 2d88004
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 2d88004

Please sign in to comment.