diff --git a/skimage/io/collection.py b/skimage/io/collection.py index 49d6aa25..aab7cb86 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -135,7 +135,8 @@ class ImageCollection(object): ic = ImageCollection('/tmp/*.png', load_func=imread_convert) For files with multiple images, the images will be flattened into a list - and added to the list of available images. + and added to the list of available images. In this case, ``load_func`` + should accept the keyword argument ``img_num``. Examples -------- @@ -165,7 +166,7 @@ class ImageCollection(object): self._numframes = self._find_images() else: self._files = load_pattern - self._numframes = len(load_pattern) + self._numframes = len(self._files) self._frame_index = None if conserve_memory: @@ -254,13 +255,16 @@ class ImageCollection(object): if ((self.conserve_memory and n != self._cached) or (self.data[idx] is None)): + kwargs = self.load_func_kwargs if self._frame_index: fname, img_num = self._frame_index[n] - self.data[idx] = self.load_func(fname, img_num=img_num, - **self.load_func_kwargs) + if img_num > 0: + self.data[idx] = self.load_func(fname, img_num=img_num, + **kwargs) + else: + self.data[idx] = self.load_func(fname, **kwargs) else: - self.data[idx] = self.load_func(self.files[n], - **self.load_func_kwargs) + self.data[idx] = self.load_func(self.files[n], **kwargs) self._cached = n return self.data[idx] diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py index 81f8058a..238229d1 100644 --- a/skimage/io/tests/test_freeimage.py +++ b/skimage/io/tests/test_freeimage.py @@ -1,6 +1,7 @@ import os import skimage as si import skimage.io as sio +from skimage import data_dir import numpy as np from numpy.testing import * @@ -112,5 +113,17 @@ def test_metadata(): assert meta[1][('EXIF_MAIN', 'Software')].startswith('I') +@skipif(not FI_available) +def test_collection(): + pattern = [os.path.join(data_dir, pic) + for pic in ['camera.png', 'color.png', 'multipage.tif']] + images = sio.ImageCollection(pattern[:-1]) + assert len(images) == 2 + assert len(images[:]) == 2 + + images = sio.ImageCollection(pattern) + assert len(images) == 3 + assert len(images[:]) == 3 + if __name__ == "__main__": run_module_suite()