From 883baa03282c8bf97fbd327e467850314b941fcd Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 9 May 2015 09:10:34 -0500 Subject: [PATCH 1/8] Add a multi-page rbg tiff file --- skimage/data/multipage_rgb.tif | Bin 0 -> 5278 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 skimage/data/multipage_rgb.tif diff --git a/skimage/data/multipage_rgb.tif b/skimage/data/multipage_rgb.tif new file mode 100644 index 0000000000000000000000000000000000000000..6832c368f417fb4f96c27aada57e9290618a12ee GIT binary patch literal 5278 zcmb7Ic|4Wfw?Br2&>^BhrBp(ap-6lYk%)?tB8o^Q(JSJWQieuJk||SZGE^v29b*WY z$Afbmhl6t*QB>%?PxrpR&+VW4$NfBeefIjSXYIB3v-jF-eb=*Vmm*1!L?V$%zexNf z9)jZ~h{W?hyzp=K51yBYkHkmF>Ivc__}}`!N#I}p_gFC=UXldSw-6*wjQy=YNRY%I z`cnV$zuztX!@l&t`V)kF`5!hn6E;N%*)M|B2wsxtg^7vx|6wPPSgihkKi+Omo-Voy zYRc-$n^lRnfwY3q)#E3T9OjcqtB6VeL+4MPIC0|SX%`hwKa!g2X3cG?MAKfSs=ZBB zYnz%jsUvQQbm*QBu#%`AIT26|a`Int^O`A$%wMvf*}y@5f2U=iGaZ#K;|r3G4j@eB z>$zvzpRoGD?2YBydQsX~;+(aj5!udbi@qwRf}JH0Z@GhpMEx?RaP?33sE5#(Fd5WMvv4itVeI(CPRl@e8YQ`8|O zeGn=5ekmWB(PC%#Qn~QYII45;`4DDD=K9Z^pdi4dVZuSB1+H#iBR;x~fj2J9rSNtW zmWEGVxw#w&|C0Oi&9Mzro3A8#rSu{m!GFU%YH zKVFAer$sMIXWHSlOkMOwX*bfy)fOJDpRqUMuk1WcCc49W%$W26tdzW_I5fpZ`IfOo zr?)Y|^R&or>fIO)Zf0z~!#jpG)(fPg3`d|~deDhvT!L|nJp-rDRAS+s3o|w~bSxP( z8KFF7LcyW>&HA`D(D=h9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Qiu=XuaD}_6y zb8T<}7q7P|X7>biK+rz?l1@hnYG3=DKkG9B5g&siQGfU1SA!vszYrCrBU;fEZYy2{ zx+ji4po67ebnv8nF$9)wX)vI0p)eM`&Hmd5qRu(~&3mW|6z9-afoFzcrEu8)TU8;{ zEb2GDHX-84=5N!H`4P~qFbuZI;vn*#Ys-`BKCHXDSDaqgi^xOGb{nZ}Q07>xT(+o# zYq>+W@<}>Kq`9RDT91*KT6kEctO?~GVix&f? za{GHu4Jr*|ye8)_uc|hzDpYdWJMTTnZaR$|;(tSdsOgUGxNfZVn9o;fN<&z%lGcd# z0On6=?LW1Uh1mxuy=31HW9ruYDB(wJSP88&5l?ynjk1q=8W%@UezyBguy`v-8hiG< zb85ng;;Hqu3)_$s7@}Cj^A=;v=cf89dXXa}_vPTNGE8R=mdRlRKKy*odM&##^L=NW z@+=2~Z-`SyLdJk0w}r@y@84d1 zAVkF1qSw5f)t#lF%q&T98A`zvzfnl1Ee9!2$U9bxwBS)b?`3sO23$gFY-P)d@0OXb z>X_DsPYzqM-Z}I`N1uOQ0fne%Qn^l^Q;~?t>OGj;T#r?-Nt>N{jv&6D3cDm-y`zy Date: Sat, 9 May 2015 09:32:33 -0500 Subject: [PATCH 2/8] Fix handling of img_num=0 cases --- skimage/io/_plugins/pil_plugin.py | 3 +++ skimage/io/collection.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 6fecbc2a..f7c10789 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -114,6 +114,9 @@ def pil_to_ndarray(im, dtype=None, img_num=None): frames.append(frame) i += 1 + if img_num is not None: + break + if hasattr(im, 'fp') and im.fp: im.fp.close() diff --git a/skimage/io/collection.py b/skimage/io/collection.py index 8812740e..bf8c58da 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -254,7 +254,7 @@ class ImageCollection(object): kwargs = self.load_func_kwargs if self._frame_index: fname, img_num = self._frame_index[n] - if img_num > 0: + if img_num is not None: self.data[idx] = self.load_func(fname, img_num=img_num, **kwargs) else: From d351c13c2d9d873dbd408a6721bc3773c4d9876f Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 9 May 2015 09:32:56 -0500 Subject: [PATCH 3/8] Add tests for multipage_rgb tif --- skimage/io/tests/test_imread.py | 10 ++++++++-- skimage/io/tests/test_multi_image.py | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index c8d380a5..be6e4d32 100644 --- a/skimage/io/tests/test_imread.py +++ b/skimage/io/tests/test_imread.py @@ -69,12 +69,18 @@ def test_imread_separate_channels(): f = NamedTemporaryFile(suffix='.tif') fname = f.name f.close() - imsave(fname, x, plugin='tifffile') - img = imread(fname, plugin='tifffile') + imsave(fname, x) + img = imread(fname) os.remove(fname) assert img.shape == (16, 8, 3), img.shape +@skipif(not imread_available) +def test_imread_multipage_rgb_tif(): + img = imread(os.path.join(data_dir, 'multipage_rgb.png')) + assert img.shape == (10, 10, 3), img.shape + + class TestSave: def roundtrip(self, x, scaling=1): f = NamedTemporaryFile(suffix='.png') diff --git a/skimage/io/tests/test_multi_image.py b/skimage/io/tests/test_multi_image.py index 04a51ec6..742d0938 100644 --- a/skimage/io/tests/test_multi_image.py +++ b/skimage/io/tests/test_multi_image.py @@ -4,6 +4,7 @@ import numpy as np from numpy.testing import assert_raises, assert_equal, assert_allclose from skimage import data_dir +from skimage.io import use_plugin from skimage.io.collection import MultiImage, ImageCollection import six @@ -14,7 +15,8 @@ class TestMultiImage(): def setUp(self): # This multipage TIF file was created with imagemagick: # convert im1.tif im2.tif -adjoin multipage.tif - paths = [os.path.join(data_dir, 'multipage.tif'), + use_plugin('pil') + paths = [os.path.join(data_dir, 'multipage_rgb.tif'), os.path.join(data_dir, 'no_time_for_that.gif')] self.imgs = [MultiImage(paths[0]), MultiImage(paths[0], conserve_memory=False), @@ -24,6 +26,12 @@ class TestMultiImage(): ImageCollection(paths[1], conserve_memory=False), ImageCollection('%s:%s' % (paths[0], paths[1]))] + def test_shapes(self): + img = self.imgs[-1] + imgs = img[:] + assert imgs[0].shape == imgs[1].shape + assert imgs[0].shape == (10, 10, 3) + def test_len(self): assert len(self.imgs[0]) == len(self.imgs[1]) == 2 assert len(self.imgs[2]) == len(self.imgs[3]) == 24 From 5f067ff43b79d430458b517d229e5235c1734dcb Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 9 May 2015 10:22:48 -0500 Subject: [PATCH 4/8] Move tests to pil plugin and fix file path --- skimage/io/tests/test_imread.py | 20 -------------------- skimage/io/tests/test_pil.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index be6e4d32..9f045d12 100644 --- a/skimage/io/tests/test_imread.py +++ b/skimage/io/tests/test_imread.py @@ -61,26 +61,6 @@ 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) - img = imread(fname) - os.remove(fname) - assert img.shape == (16, 8, 3), img.shape - - -@skipif(not imread_available) -def test_imread_multipage_rgb_tif(): - img = imread(os.path.join(data_dir, 'multipage_rgb.png')) - assert img.shape == (10, 10, 3), img.shape - - class TestSave: def roundtrip(self, x, scaling=1): f = NamedTemporaryFile(suffix='.png') diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 77957053..1ac184a2 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -50,6 +50,24 @@ def test_imread_flatten(): assert np.sctype2char(img.dtype) in np.typecodes['AllInteger'] +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) + img = imread(fname) + os.remove(fname) + assert img.shape == (16, 8, 3), img.shape + + +def test_imread_multipage_rgb_tif(): + img = imread(os.path.join(data_dir, 'multipage_rgb.tif')) + assert img.shape == (10, 10, 3), img.shape + + def test_imread_palette(): img = imread(os.path.join(data_dir, 'palette_gray.png')) assert img.ndim == 2 From b479eabd7e4119994c7d27ff6b54302c629c4211 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 9 May 2015 16:56:10 -0500 Subject: [PATCH 5/8] Fix failing pil test --- skimage/io/tests/test_pil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 1ac184a2..cd4e8939 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -65,7 +65,7 @@ def test_imread_separate_channels(): def test_imread_multipage_rgb_tif(): img = imread(os.path.join(data_dir, 'multipage_rgb.tif')) - assert img.shape == (10, 10, 3), img.shape + assert img.shape == (2, 10, 10, 3), img.shape def test_imread_palette(): From 0491326d22ea4f5877a09611670ff7a63302a4fb Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sun, 10 May 2015 20:49:46 -0500 Subject: [PATCH 6/8] Remove explicit call to use_plugin --- skimage/io/tests/test_multi_image.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/skimage/io/tests/test_multi_image.py b/skimage/io/tests/test_multi_image.py index 742d0938..be7d37d2 100644 --- a/skimage/io/tests/test_multi_image.py +++ b/skimage/io/tests/test_multi_image.py @@ -4,7 +4,6 @@ import numpy as np from numpy.testing import assert_raises, assert_equal, assert_allclose from skimage import data_dir -from skimage.io import use_plugin from skimage.io.collection import MultiImage, ImageCollection import six @@ -15,7 +14,6 @@ class TestMultiImage(): def setUp(self): # This multipage TIF file was created with imagemagick: # convert im1.tif im2.tif -adjoin multipage.tif - use_plugin('pil') paths = [os.path.join(data_dir, 'multipage_rgb.tif'), os.path.join(data_dir, 'no_time_for_that.gif')] self.imgs = [MultiImage(paths[0]), From e240dff3958f537079297f061479b3093ddcb46c Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 11 May 2015 20:54:46 -0500 Subject: [PATCH 7/8] Reinstate explicit plugin --- skimage/io/tests/test_multi_image.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/skimage/io/tests/test_multi_image.py b/skimage/io/tests/test_multi_image.py index be7d37d2..30ab6634 100644 --- a/skimage/io/tests/test_multi_image.py +++ b/skimage/io/tests/test_multi_image.py @@ -3,6 +3,7 @@ import os import numpy as np from numpy.testing import assert_raises, assert_equal, assert_allclose +from skimage.io import use_plugin from skimage import data_dir from skimage.io.collection import MultiImage, ImageCollection @@ -14,6 +15,7 @@ class TestMultiImage(): def setUp(self): # This multipage TIF file was created with imagemagick: # convert im1.tif im2.tif -adjoin multipage.tif + use_plugin('pil') paths = [os.path.join(data_dir, 'multipage_rgb.tif'), os.path.join(data_dir, 'no_time_for_that.gif')] self.imgs = [MultiImage(paths[0]), From dfaab1957e7c933b7166e76d30dd7887cfb037d7 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 12 May 2015 21:10:23 -0500 Subject: [PATCH 8/8] Add a multipage tiff test and cleanup test_tifffile --- skimage/io/tests/test_tifffile.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/skimage/io/tests/test_tifffile.py b/skimage/io/tests/test_tifffile.py index fc958145..57047f0b 100644 --- a/skimage/io/tests/test_tifffile.py +++ b/skimage/io/tests/test_tifffile.py @@ -1,6 +1,6 @@ import os -import skimage as si -import skimage.io as sio +from ... import data_dir +from .. import imread, imsave, use_plugin, reset_plugins import numpy as np from numpy.testing import ( @@ -8,38 +8,42 @@ from numpy.testing import ( from tempfile import NamedTemporaryFile -_plugins = sio.plugin_order() -sio.use_plugin('tifffile') - -np.random.seed(0) +def setup(): + use_plugin('tifffile') + np.random.seed(0) def teardown(): - sio.reset_plugins() + reset_plugins() def test_imread_uint16(): - expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) - img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16.tif')) + expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) + img = imread(os.path.join(data_dir, 'chessboard_GRAY_U16.tif')) assert img.dtype == np.uint16 assert_array_almost_equal(img, expected) def test_imread_uint16_big_endian(): - expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) - img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16B.tif')) + expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) + img = imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif')) assert img.dtype == np.uint16 assert_array_almost_equal(img, expected) +def test_imread_multipage_rgb_tif(): + img = imread(os.path.join(data_dir, 'multipage_rgb.tif')) + assert img.shape == (2, 10, 10, 3), img.shape + + class TestSave: def roundtrip(self, dtype, x): f = NamedTemporaryFile(suffix='.tif') fname = f.name f.close() - sio.imsave(fname, x) - y = sio.imread(fname) + imsave(fname, x) + y = imread(fname) assert_array_equal(x, y) def test_imsave_roundtrip(self):