Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tifffile to read compressed tif files #93

Merged
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
27 changes: 27 additions & 0 deletions test/util/test_imageio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import tempfile
import unittest

import numpy as np
import tifffile


class TestImageRead(unittest.TestCase):
def test_read_memap(self):
from torch_em.util.image import load_image, supports_memmap

with tempfile.TemporaryDirectory() as td:
tifffile.imwrite(os.path.join(td, "test.tif"), np.zeros((10, 10, 2)))
self.assert_(supports_memmap(os.path.join(td, "test.tif")))
data = load_image(os.path.join(td, "test.tif"))
self.assertEqual(data.shape, (10, 10, 2))

def test_read_copressed(self):
from torch_em.util.image import load_image

with tempfile.TemporaryDirectory() as td:
tifffile.imwrite(os.path.join(td, "test.tif"), np.zeros((10, 10, 2)), compression="ADOBE_DEFLATE")
data = load_image(os.path.join(td, "test.tif"))
self.assertEqual(data.shape, (10, 10, 2))


4 changes: 3 additions & 1 deletion torch_em/util/image.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TODO this should be partially refactored into elf.io before the next elf release
# and then be used in image_stack_wrapper as welll
import os
import imageio
import imageio.v2 as imageio

try:
import tifffile
Expand All @@ -27,6 +27,8 @@ def supports_memmap(image_path):
def load_image(image_path):
if supports_memmap(image_path):
return tifffile.memmap(image_path, mode='r')
elif tifffile is not None and os.path.splitext(image_path)[1].lower() in {".tiff", ".tif"}:
return tifffile.imread(image_path)
else:
# TODO handle multi-channel images
return imageio.imread(image_path)