Repository: scikit-image/scikit-image

Issue:
ImageCollection broken when using freeimage
Basically this one below breaks, with scikit-image 0.12 and freeimage-3.15.4.  

``` python
io.use_plugin('freeimage')
ic = ImageCollection( join(dir, '*.png' ) )
img = ic[0]
```

``` python
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-cb6c412db3ff> in <module>()
----> 1 create_single_epi(dir, rows/2, pix_shift = 0 )

/export/home/mgutsche/scripts/HelperFunctions/create_epis.py in create_single_epi(dir, row, pix_shift)
     19     io.use_plugin('freeimage')
     20     ic = ImageCollection( join(dir, '*.png' ) )
---> 21     rows, cols, channels = ic[0].shape
     22     if channels >= 4:
     23         channels = 3

/export/home/mgutsche/anaconda/lib/python2.7/site-packages/skimage/io/collection.pyc in __getitem__(self, n)
    258                     fname, img_num = self._frame_index[n]
    259                     self.data[idx] = self.load_func(fname, img_num=img_num,
--> 260                                                     **self.load_func_kwargs)
    261                 else:
    262                     self.data[idx] = self.load_func(self.files[n],

/export/home/mgutsche/anaconda/lib/python2.7/site-packages/skimage/io/_io.pyc in imread(fname, as_grey, plugin, flatten, **plugin_args)
     95 
     96     with file_or_url_context(fname) as fname:
---> 97         img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
     98 
     99     if as_grey and getattr(img, 'ndim', 0) >= 3:

/export/home/mgutsche/anaconda/lib/python2.7/site-packages/skimage/io/manage_plugins.pyc in call_plugin(kind, *args, **kwargs)
    205                                (plugin, kind))
    206 
--> 207     return func(*args, **kwargs)
    208 
    209 

TypeError: imread() got an unexpected keyword argument 'img_num'
```



Discussion:

Resolving patch:
diff --git a/skimage/io/collection.py b/skimage/io/collection.py
--- a/skimage/io/collection.py
+++ b/skimage/io/collection.py
@@ -135,7 +135,8 @@ def imread_convert(f):
       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 @@ def __init__(self, load_pattern, conserve_memory=True, load_func=None,
             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 @@ def __getitem__(self, n):
 
             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]


Test patch:
diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py
--- 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()