Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bioio/tests/writers/test_ome_tiff_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,21 @@ def test_ome_tiff_writer_common_metadata(
scene = tiff.series[i]
assert scene.shape == read_shapes[i]
assert scene.pages.axes == read_dim_order[i]


def test_ome_tiff_writer_custom_compression(tmp_path: pathlib.Path) -> None:
# Create array
arr = np.random.rand(5, 16, 16)

# Construct save end point
save_uri = tmp_path / "e.ome.tiff"

# Normal save
OmeTiffWriter.save(
arr,
save_uri,
tifffile_kwargs={
"compression": "zlib",
"compressionargs": {"level": 8},
},
)
25 changes: 24 additions & 1 deletion bioio/writers/ome_tiff_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
# calculate it but for now this is a stopgap working value
BIGTIFF_BYTE_LIMIT = 2**21

# Default tifffile.write kwargs
DEFAULT_TIFF_WRITE_KWARGS = {"compression": TIFF.COMPRESSION.ADOBE_DEFLATE}


class OmeTiffWriter(Writer):
@staticmethod
Expand All @@ -46,6 +49,7 @@ def save(
Union[List[List[int]], List[Optional[List[List[int]]]]]
] = None,
fs_kwargs: Dict[str, Any] = {},
tifffile_kwargs: Dict[str, Any] = DEFAULT_TIFF_WRITE_KWARGS,
**kwargs: Any,
) -> None:
"""
Expand Down Expand Up @@ -102,6 +106,13 @@ def save(
Any specific keyword arguments to pass down to the fsspec created
filesystem.
Default: {}
tifffile_kwargs: Dict[str, Any]
Any specific keyword arguments to pass down to tifffile write function.
Do not use these kwargs to provide the image `description`,
`photometric` configuration, or `planarconfig` parameters as those are all
determined via the image and metadata provided from caller.
Default: DEFAULT_TIFF_WRITE_KWARGS
Compression using ADOBE_DEFLATE (ZLIB)

Raises
------
Expand Down Expand Up @@ -130,6 +141,18 @@ def save(
... dim_order="CZYX", # this single value will be repeated to each image
... channel_names=[["C00","C01","C02"],["C10","C11","C12"]]
... )

Write data with custom compression scheme

>>> image = numpy.ndarray([1, 10, 3, 1024, 2048])
... OmeTiffWriter.save(
... image,
... "file.ome.tif",
... tifffile_kwargs={
... "compression": "zlib",
... "compressionargs": {"level": 8},
... },
... )
"""
# Resolve final destination
fs, path = biob.io.pathlike_to_fs(uri, fs_kwargs=fs_kwargs)
Expand Down Expand Up @@ -301,7 +324,7 @@ def save(
photometric=photometric,
metadata=None,
planarconfig=planarconfig,
compression=TIFF.COMPRESSION.ADOBE_DEFLATE,
**tifffile_kwargs,
)

tif.close()
Expand Down