diff --git a/skimage/io/_io.py b/skimage/io/_io.py index 2f957ac2..8dbc3600 100644 --- a/skimage/io/_io.py +++ b/skimage/io/_io.py @@ -53,8 +53,16 @@ def imread(fname, as_grey=False, plugin=None, flatten=None, with file_or_url_context(fname) as fname: img = call_plugin('imread', fname, plugin=plugin, **plugin_args) - if as_grey and getattr(img, 'ndim', 0) >= 3: - img = rgb2grey(img) + if not hasattr(img, 'ndim'): + return img + + if img.ndim > 2: + if img.shape[-1] not in (3, 4) and img.shape[-3] in (3, 4): + img = np.swapaxes(img, -1, -3) + img = np.swapaxes(img, -2, -3) + + if as_grey: + img = rgb2grey(img) return img diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index 759e8c67..c8d380a5 100644 --- a/skimage/io/tests/test_imread.py +++ b/skimage/io/tests/test_imread.py @@ -1,3 +1,4 @@ +import os import os.path import numpy as np from numpy.testing import * @@ -60,6 +61,20 @@ def test_bilevel(): assert_array_equal(img.astype(bool), expected) +@skipif(not imread_available) +def test_imread_separate_channels(): + # Test that imread returns RGBA values contiguously even when they are + # stored in separate planes. + x = np.random.rand(3, 16, 8) + f = NamedTemporaryFile(suffix='.tif') + fname = f.name + f.close() + imsave(fname, x, plugin='tifffile') + img = imread(fname, plugin='tifffile') + os.remove(fname) + assert img.shape == (16, 8, 3), img.shape + + class TestSave: def roundtrip(self, x, scaling=1): f = NamedTemporaryFile(suffix='.png')