Fix handling of multi-image gifs and add test

This commit is contained in:
Steven Silvester
2014-12-12 07:46:16 -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]