mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-02 15:23:16 +08:00
Fix handling of multi-image gifs and add test
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user