From 3c8bf02fa5a9749f5de1bc4bbd6b2e2abb7d1a25 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Thu, 7 May 2015 15:03:25 -0700 Subject: [PATCH 1/4] Fix skimage.io.imread returns incorrect dimensions Fixes #1496 --- skimage/io/_io.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/skimage/io/_io.py b/skimage/io/_io.py index 2f957ac2..f8305aaf 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, -1, -2) + + if as_grey: + img = rgb2grey(img) return img From 47cfcb4b8b2cf4cd483427b5d1aaddee1fe74119 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Thu, 7 May 2015 15:07:59 -0700 Subject: [PATCH 2/4] TST skimage.io.imread returns incorrect dimensions --- skimage/io/tests/test_imread.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index 759e8c67..4f172ebb 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.zeros((3, 16, 8), np.uint8) + f = NamedTemporaryFile(suffix='.tif') + fname = f.name + f.close() + imsave(fname, x, plugin='tifffile') + img = imread(fname) + os.remove(fname) + assert img.shape == (16, 8, 3), img.shape + + class TestSave: def roundtrip(self, x, scaling=1): f = NamedTemporaryFile(suffix='.png') From 3d3351b9faaee80b9c848171e6933cecde069343 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Thu, 7 May 2015 17:34:05 -0700 Subject: [PATCH 3/4] Fix failing test --- skimage/io/_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/io/_io.py b/skimage/io/_io.py index f8305aaf..8dbc3600 100644 --- a/skimage/io/_io.py +++ b/skimage/io/_io.py @@ -59,7 +59,7 @@ def imread(fname, as_grey=False, plugin=None, flatten=None, 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, -1, -2) + img = np.swapaxes(img, -2, -3) if as_grey: img = rgb2grey(img) From 549def734eaf6de2c0bd9929b83bdf9a9469d55c Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Thu, 7 May 2015 17:35:48 -0700 Subject: [PATCH 4/4] Fix failing test --- skimage/io/tests/test_imread.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index 4f172ebb..c8d380a5 100644 --- a/skimage/io/tests/test_imread.py +++ b/skimage/io/tests/test_imread.py @@ -65,12 +65,12 @@ def test_bilevel(): def test_imread_separate_channels(): # Test that imread returns RGBA values contiguously even when they are # stored in separate planes. - x = np.zeros((3, 16, 8), np.uint8) + x = np.random.rand(3, 16, 8) f = NamedTemporaryFile(suffix='.tif') fname = f.name f.close() imsave(fname, x, plugin='tifffile') - img = imread(fname) + img = imread(fname, plugin='tifffile') os.remove(fname) assert img.shape == (16, 8, 3), img.shape