Allow multiple frames in PIL data in imread

This commit is contained in:
Steven Silvester
2014-11-07 15:22:19 +02:00
committed by Stefan van der Walt
parent 39d9dcf20e
commit 4298b8babc
2 changed files with 21 additions and 3 deletions
+17 -1
View File
@@ -45,7 +45,23 @@ def imread(fname, dtype=None):
site = "http://pillow.readthedocs.org/en/latest/installation.html#external-libraries"
raise ValueError('Could not load "%s"\nPlease see documentation at: %s' % (fname, site))
else:
return pil_to_ndarray(im, dtype)
return _get_pil_frames(im, dtype)
def _get_pil_frames(img, dtype):
frames = []
try:
i = 0
while True:
frames.append(pil_to_ndarray(img, dtype=dtype,
close_fid=False))
i += 1
img.seek(i)
except EOFError:
pass
finally:
img.fp.close()
return np.dstack(frames)
def pil_to_ndarray(im, dtype=None, close_fid=True):
+4 -2
View File
@@ -345,6 +345,8 @@ class ImageCollection(object):
if load_func is None:
from ._io import imread
self.load_func = imread
# treat each one as a multi image, then we access
# them from there
else:
self.load_func = load_func
@@ -412,7 +414,7 @@ class ImageCollection(object):
def _check_imgnum(self, n):
"""Check that the given image number is valid."""
num = len(self.files)
num = len(self.nimages)
if -num <= n < num:
n = n % num
else:
@@ -427,7 +429,7 @@ class ImageCollection(object):
def __len__(self):
"""Number of images in collection."""
return len(self.files)
return self.nimages
def __str__(self):
return str(self.files)