diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index e1390685..f8d6488d 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -48,7 +48,7 @@ def imread(fname, dtype=None): return pil_to_ndarray(im, dtype) -def pil_to_ndarray(im, dtype=None): +def pil_to_ndarray(im, dtype=None, close_fid=True): """Import a PIL Image object to an ndarray, in memory. Parameters @@ -74,7 +74,7 @@ def pil_to_ndarray(im, dtype=None): elif 'A' in im.mode: im = im.convert('RGBA') im = np.array(im, dtype=dtype) - if fp is not None: + if fp is not None and close_fid: fp.close() return im diff --git a/skimage/io/collection.py b/skimage/io/collection.py index b5821c01..15ed5cfb 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -131,7 +131,8 @@ class MultiImage(object): self._frames = self._getallframes(img) self._numframes = len(self._frames) - img.close() + if not self.tif_img: + img.fp.close() @property def filename(self): @@ -178,11 +179,15 @@ class MultiImage(object): try: i = 0 while True: - frames.append(pil_to_ndarray(img, dtype=self._dtype)) + frames.append(pil_to_ndarray(img, dtype=self._dtype, + close_fid=False)) i += 1 img.seek(i) except EOFError: - return frames + pass + finally: + img.fp.close() + return frames def __getitem__(self, n): """Return the n-th frame as an array.