Skip to content

Commit

Permalink
add tifffile backend support for image io (#868)
Browse files Browse the repository at this point in the history
* add tifffile backend support for imageio

* add tifffile package for test requirements

* add tifffile package for test requirements

* small tiffile
  • Loading branch information
sshuair authored Mar 12, 2021
1 parent 9ba1f76 commit 73bff4e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
24 changes: 18 additions & 6 deletions mmcv/image/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
except ImportError:
Image = None

try:
import tifffile
except ImportError:
tifffile = None

jpeg = None
supported_backends = ['cv2', 'turbojpeg', 'pillow']
supported_backends = ['cv2', 'turbojpeg', 'pillow', 'tifffile']

imread_flags = {
'color': IMREAD_COLOR,
Expand All @@ -36,8 +41,9 @@ def use_backend(backend):
Args:
backend (str): The image decoding backend type. Options are `cv2`,
`pillow`, `turbojpeg` (see https://github.com/lilohuang/PyTurboJPEG).
`turbojpeg` is faster but it only supports `.jpeg` file format.
`pillow`, `turbojpeg` (see https://github.com/lilohuang/PyTurboJPEG)
and `tifffile`. `turbojpeg` is faster but it only supports `.jpeg`
file format.
"""
assert backend in supported_backends
global imread_backend
Expand All @@ -51,6 +57,9 @@ def use_backend(backend):
elif imread_backend == 'pillow':
if Image is None:
raise ImportError('`Pillow` is not installed')
elif imread_backend == 'tifffile':
if tifffile is None:
raise ImportError('`tifffile` is not installed')


def _jpegflag(flag='color', channel_order='bgr'):
Expand Down Expand Up @@ -134,9 +143,9 @@ def imread(img_or_path, flag='color', channel_order='bgr', backend=None):
Note that the `turbojpeg` backened does not support `unchanged`.
channel_order (str): Order of channel, candidates are `bgr` and `rgb`.
backend (str | None): The image decoding backend type. Options are
`cv2`, `pillow`, `turbojpeg`, `None`. If backend is None, the
global imread_backend specified by ``mmcv.use_backend()`` will be
used. Default: None.
`cv2`, `pillow`, `turbojpeg`, `tifffile`, `None`.
If backend is None, the global imread_backend specified by
``mmcv.use_backend()`` will be used. Default: None.
Returns:
ndarray: Loaded image array.
Expand Down Expand Up @@ -166,6 +175,9 @@ def imread(img_or_path, flag='color', channel_order='bgr', backend=None):
img = Image.open(img_or_path)
img = _pillow2array(img, flag, channel_order)
return img
elif backend == 'tifffile':
img = tifffile.imread(img_or_path)
return img
else:
flag = imread_flags[flag] if is_str(flag) else flag
img = cv2.imread(img_or_path, flag)
Expand Down
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ onnxoptimizer
onnxruntime==1.4.0
pytest
PyTurboJPEG
tiffile
Binary file added tests/data/uint16-5channel.tif
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test_image/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setup_class(cls):
cls.palette_img_path = osp.join(cls.data_dir, 'palette.gif')
cls.exif_img_path = osp.join(cls.data_dir, 'color_exif.jpg')
cls.img = cv2.imread(cls.img_path)
cls.tiff_path = osp.join(cls.data_dir, 'uint16-5channel.tif')

def assert_img_equal(self, img, ref_img, ratio_thr=0.999):
assert img.shape == ref_img.shape
Expand Down Expand Up @@ -173,6 +174,11 @@ def test_imread(self):
with pytest.raises(ValueError):
mmcv.imread(self.img_path, 'unsupported_backend')

# backend tifffile, multi channel tiff file(> 4 channels).
mmcv.use_backend('tifffile')
img_tifffile = mmcv.imread(self.tiff_path)
assert img_tifffile.shape == (200, 150, 5)

mmcv.use_backend('cv2')

# consistent exif behaviour
Expand Down

0 comments on commit 73bff4e

Please sign in to comment.