diff --git a/skimage/data/multipage_rgb.tif b/skimage/data/multipage_rgb.tif new file mode 100644 index 00000000..6832c368 Binary files /dev/null and b/skimage/data/multipage_rgb.tif differ 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: diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index c8d380a5..9f045d12 100644 --- a/skimage/io/tests/test_imread.py +++ b/skimage/io/tests/test_imread.py @@ -61,20 +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, 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') diff --git a/skimage/io/tests/test_multi_image.py b/skimage/io/tests/test_multi_image.py index 04a51ec6..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,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 diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 77957053..cd4e8939 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 == (2, 10, 10, 3), img.shape + + def test_imread_palette(): img = imread(os.path.join(data_dir, 'palette_gray.png')) assert img.ndim == 2 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):