mirror of https://github.com/open-mmlab/mmcv.git
add tifffile backend support for image io (#868)
* add tifffile backend support for imageio * add tifffile package for test requirements * add tifffile package for test requirements * small tiffilepull/898/head
parent
9ba1f76005
commit
73bff4ead1
|
@ -19,8 +19,13 @@ try:
|
|||
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,
|
||||
|
@ -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
|
||||
|
@ -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'):
|
||||
|
@ -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.
|
||||
|
@ -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)
|
||||
|
|
|
@ -5,3 +5,4 @@ onnxoptimizer
|
|||
onnxruntime==1.4.0
|
||||
pytest
|
||||
PyTurboJPEG
|
||||
tiffile
|
||||
|
|
Binary file not shown.
|
@ -28,6 +28,7 @@ class TestIO:
|
|||
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
|
||||
|
@ -173,6 +174,11 @@ class TestIO:
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue