Skip to content

Commit

Permalink
Merge pull request #67 from thewtex/cli-io-wasm
Browse files Browse the repository at this point in the history
Cli io wasm
  • Loading branch information
thewtex authored Jan 26, 2024
2 parents f16f650 + f2a57ca commit b114c34
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
8 changes: 8 additions & 0 deletions ngff_zarr/cli_input_to_ngff_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def cli_input_to_ngff_image(
if backend is ConversionBackend.ZARR_ARRAY:
arr = zarr.open_array(input[0], mode="r")
return to_ngff_image(arr)
if backend is ConversionBackend.ITKWASM:
try:
import itkwasm_image_io
except ImportError:
print("[red]Please install the [i]itkwasm-image-io[/i] package.")
sys.exit(1)
image = itkwasm_image_io.imread(input[0])
return itk_image_to_ngff_image(image)
if backend is ConversionBackend.ITK:
try:
import itk
Expand Down
45 changes: 45 additions & 0 deletions ngff_zarr/detect_cli_io_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
conversion_backends = [
("NGFF_ZARR", "ngff_zarr"),
("ZARR_ARRAY", "zarr"),
("ITKWASM", "itkwasm_image_io"),
("ITK", "itk"),
("TIFFFILE", "tifffile"),
("IMAGEIO", "imageio"),
Expand All @@ -24,15 +25,58 @@ def detect_cli_io_backend(input: List[str]) -> ConversionBackend:
if extension in ngff_zarr_supported_extensions:
return ConversionBackend.NGFF_ZARR

itkwasm_supported_extensions = (
".bmp",
".dcm",
".gipl",
".gipl.gz",
".hdf5",
".jpg",
".jpeg",
".iwi",
".iwi.cbor",
".iwi.cbor.zst",
".lsm",
".mnc",
".mnc.gz",
".mnc2",
".mgh",
".mhz",
".mha",
".mhd",
".mrc",
".nia",
".nii",
".nii.gz",
".hdr",
".nrrd",
".nhdr",
".png",
".pic",
".vtk",
".aim",
".isq",
".fdf",
)
if (
extension in itkwasm_supported_extensions
and len(input) == 1
and Path(input[0]).is_file()
and Path(input[0]).stat().st_size < 2e9
):
return ConversionBackend.ITKWASM

itk_supported_extensions = (
".bmp",
".dcm",
".gipl",
".gipl.gz",
".hdf5",
".jpg",
".jpeg",
".iwi",
".iwi.cbor",
".iwi.cbor.zst",
".lsm",
".mnc",
".mnc.gz",
Expand All @@ -52,6 +96,7 @@ def detect_cli_io_backend(input: List[str]) -> ConversionBackend:
".pic",
".vtk",
".isq", # Requires pip install itk-ioscanco,
".aim", # Requires pip install itk-ioscanco,
".fdf", # Requires pip install itk-iofdf
)

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ cli = [
"dask[distributed]",
"itk-filtering>=5.3.0",
"itk-io>=5.3.0",
"itkwasm-image-io",
"imageio",
"tifffile",
"imagecodecs",
Expand All @@ -76,6 +77,7 @@ test = [
"pooch",
"itkwasm",
"itk-io>=5.3.0",
"itkwasm-image-io",
"itk-filtering>=5.3.0",
"tifffile",
"jsonschema",
Expand Down
11 changes: 5 additions & 6 deletions test/_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys
from pathlib import Path

import itk
import pooch
import pytest
from itkwasm_image_io import imread
from ngff_zarr import itk_image_to_ngff_image, to_ngff_zarr
from zarr.storage import DirectoryStore, MemoryStore

Expand All @@ -27,24 +27,23 @@ def input_images():
)
result = {}

image = itk.imread(test_data_dir / "input" / "cthead1.png")
image = imread(test_data_dir / "input" / "cthead1.png")
image_ngff = itk_image_to_ngff_image(image)
result["cthead1"] = image_ngff

result["lung_series"] = test_data_dir / "input" / "lung_series" / "*"

image = itk.imread(
image = imread(
test_data_dir / "input" / "brain_two_components.nrrd",
itk.VariableLengthVector[itk.SS],
)
image_ngff = itk_image_to_ngff_image(image)
result["brain_two_components"] = image_ngff

image = itk.imread(test_data_dir / "input" / "2th_cthead1.png")
image = imread(test_data_dir / "input" / "2th_cthead1.png")
image_ngff = itk_image_to_ngff_image(image)
result["2th_cthead1"] = image_ngff

image = itk.imread(test_data_dir / "input" / "MR-head.nrrd")
image = imread(test_data_dir / "input" / "MR-head.nrrd")
image_ngff = itk_image_to_ngff_image(image)
result["MR-head"] = image_ngff

Expand Down

0 comments on commit b114c34

Please sign in to comment.