From 296e4926584d6c82f6ec0c2dce98e8ca6dfe41c2 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 8 Dec 2014 21:21:44 -0600 Subject: [PATCH] Fix handling of multi-image gifs and add test --- skimage/io/_plugins/pil_plugin.py | 17 ++++++++++++----- skimage/io/tests/test_pil.py | 11 ++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 42061057..3fff941e 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -64,6 +64,7 @@ def pil_to_ndarray(im, dtype=None, img_num=None): """ frames = [] + grayscale = None i = 0 while 1: try: @@ -71,23 +72,29 @@ def pil_to_ndarray(im, dtype=None, img_num=None): except EOFError: break - # seeking must be done sequentially - if not img_num is None and not i == img_num: + frame = im + + if not img_num is None and img_num != i: + im.getdata()[0] i += 1 continue - frame = im if im.mode == 'P': - if _palette_is_grayscale(im): + if grayscale is None: + grayscale = _palette_is_grayscale(im) + + if grayscale: frame = im.convert('L') else: frame = im.convert('RGB') + elif im.mode == '1': frame = im.convert('L') elif 'A' in im.mode: frame = im.convert('RGBA') + if im.mode.startswith('I;16'): shape = im.size dtype = '>u2' if im.mode.endswith('B') else ' 1: + if img_num is None: return np.array(frames) else: return frames[0] diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 02b3a4b8..06bb3dd1 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -170,6 +170,14 @@ def test_all_mono(): mono_check('pil', 'tiff') +def test_multi_page_gif(): + img = imread(os.path.join(data_dir, 'no_time_for_that.gif')) + assert img.shape == (24, 280, 500, 3), img.shape + img2 = imread(os.path.join(data_dir, 'no_time_for_that.gif'), + img_num=5) + assert img2.shape == (280, 500, 3) + assert_allclose(img[5], img2) + class TestSaveTIF: def roundtrip(self, dtype, x): f = NamedTemporaryFile(suffix='.tif') @@ -192,4 +200,5 @@ class TestSaveTIF: yield self.roundtrip, dtype, x if __name__ == "__main__": - run_module_suite() + #run_module_suite() + test_multi_page_gif()