Skip to content

Commit

Permalink
BUG: Support compression and compressionargs in tifffile plugin (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FirefoxMetzger committed Oct 28, 2022
1 parent 93e2919 commit 3773da4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
6 changes: 4 additions & 2 deletions imageio/core/legacy_plugin_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def legacy_get_writer(self, **kwargs):
self._request._kwargs = kwargs
return self._format.get_writer(self._request)

def write(self, ndimage, *, is_batch=None, **kwargs):
def write(self, ndimage, *, is_batch=None, metadata=None, **kwargs):
"""
Write an ndimage to the URI specified in path.
Expand All @@ -180,6 +180,8 @@ def write(self, ndimage, *, is_batch=None, **kwargs):
If True, treat the supplied ndimage as a batch of images. If False,
treat the supplied ndimage as a single image. If None, try to
determine ``is_batch`` from the ndimage's shape and ndim.
metadata : dict
The metadata passed to write alongside the image.
kwargs : ...
Further keyword arguments are passed to the writer. See
:func:`.help` to see what arguments are available for a
Expand Down Expand Up @@ -248,7 +250,7 @@ def write(self, ndimage, *, is_batch=None, **kwargs):
f"All images have to be numeric, and not `{image.dtype}`."
)

writer.append_data(image)
writer.append_data(image, metadata)

return writer.request.get_result()

Expand Down
24 changes: 24 additions & 0 deletions imageio/plugins/tifffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@
compress : int
Values from 0 to 9 controlling the level of zlib (deflate) compression.
If 0, data are written uncompressed (default).
compression : str, (int, int)
Compression scheme used while writing the image. If omitted (default) the
image is not uncompressed. Compression cannot be used to write contiguous
series. Compressors may require certain data shapes, types or value ranges.
For example, JPEG compression requires grayscale or RGB(A), uint8 or 12-bit
uint16. JPEG compression is experimental. JPEG markers and TIFF tags may not
match. Only a limited set of compression schemes are implemented. 'ZLIB' is
short for ADOBE_DEFLATE. The value is written to the Compression tag.
compressionargs:
Extra arguments passed to compression codec, e.g., compression level. Refer
to the Imagecodecs implementation for supported arguments.
predictor : bool
If True, horizontal differencing is applied before compression.
Note that using an int literal 1 actually means no prediction scheme
Expand Down Expand Up @@ -191,6 +202,8 @@
"resolution",
"description",
"compress",
"compression",
"compressionargs",
"predictor",
"volume",
"writeshape",
Expand Down Expand Up @@ -529,6 +542,17 @@ def _sanitize_meta(meta):
# 1(=NONE) translation to False expected by TiffWriter.save
if key == "predictor" and not isinstance(value, bool):
ret[key] = value > 1
elif key == "compress" and value != 0:
warnings.warn(
"The use of `compress` is deprecated. Use `compression` and `compressionargs` instead.",
DeprecationWarning,
)

if _tifffile.__version__ < "2022":
ret["compression"] = (8, value)
else:
ret["compression"] = "zlib"
ret["compressionargs"] = {"level": value}
else:
ret[key] = value
return ret
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def run(self):
"spe": [],
"swf": [],
"tifffile": ["tifffile"],
"pyav": ["av"],
"pyav": ["av!=10.0.0"],
}

cpython_only_plugins = {
Expand Down
1 change: 1 addition & 0 deletions tests/test_pyav.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_video_format_to_dtype():
"d3d11",
"d3d11va_vld",
"videotoolbox_vld",
"vaapi",
"vaapi_idct",
"opencl",
"cuda",
Expand Down
31 changes: 31 additions & 0 deletions tests/test_tifffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import pytest
import io
import warnings

import imageio.v2 as iio
import imageio.v3 as iio3
Expand Down Expand Up @@ -308,3 +309,33 @@ def test_multiple_ndimages(tmp_path):
shapes = [(4, 255, 255, 3), (255, 255, 3), (120, 73, 3)]
for image, shape in zip(iio3.imiter(tmp_path / "nightmare.tiff"), shapes):
assert image.shape == shape


def test_compression(tmp_path):
img = np.ones((128, 128))

with warnings.catch_warnings():
warnings.simplefilter("ignore")
iio.imwrite(tmp_path / "test.tiff", img, metadata={"compress": 5})

with tifffile.TiffFile(tmp_path / "test.tiff") as file:
# this should be tifffile.COMPRESSION.ADOBE_DEFLATE
# but that isn't supported by tifffile on python 3.7
assert file.pages[0].compression == 8
print("")

iio.imwrite(tmp_path / "test.tiff", img, metadata={"compression": "zlib"})
with tifffile.TiffFile(tmp_path / "test.tiff") as file:
# this should be tifffile.COMPRESSION.ADOBE_DEFLATE
# but that isn't supported by tifffile on python 3.7
assert file.pages[0].compression == 8
print("")

iio.imwrite(
tmp_path / "test.tiff",
img,
)
with tifffile.TiffFile(tmp_path / "test.tiff") as file:
# this should be tifffile.COMPRESSION.NONE
# but that isn't supported by tifffile on python 3.7
assert file.pages[0].compression == 1

0 comments on commit 3773da4

Please sign in to comment.