Fix handling of multi-image gifs and add test

This commit is contained in:
Steven Silvester
2014-12-08 21:21:44 -06:00
parent f7e984662a
commit 296e492658
2 changed files with 22 additions and 6 deletions
+12 -5
View File
@@ -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 '<u2'
@@ -105,7 +112,7 @@ def pil_to_ndarray(im, dtype=None, img_num=None):
if hasattr(im, 'fp') and im.fp:
im.fp.close()
if len(frames) > 1:
if img_num is None:
return np.array(frames)
else:
return frames[0]
+10 -1
View File
@@ -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()