Repository: scikit-image/scikit-image

Issue:
problem with ImageCollection
## Description

I have a problem with the ImageCollection when used own loader function
## Way to reproduce

Python version

> Python 2.7.11 |Anaconda custom (64-bit)|  
> [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
> scikit-image              0.12.3              np110py27_0

I have the following code

```
import cPickle as pickle
import numpy as np

import skimage.io as io
from skimage.color import rgb2gray
from skimage.transform import resize

IMAGE_SIZE = 96

def transformImage(f):
        img = io.imread(f)
        img_gray = rgb2gray(img)        
        im_res = resize(img_gray,(IMAGE_SIZE,IMAGE_SIZE))

        return im_res

if __name__ == "__main__":
    img_collections = io.ImageCollection('/home/user/work/pic/cars/*.jpg',load_func=transformImage)
        img_collections[1].shape

```

I receive the following message

> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/user/anaconda2/lib/python2.7/site-packages/skimage/io/collection.py", line 260, in **getitem**
>     **kwargs)
> TypeError: transformImage() got an unexpected keyword argument 'img_num'

if remove own  load_func(=transformImage) then everything works correctly



Discussion:
@ProgRoman, as a workaround, you can change your function definition to `transformImage(f, img_num=None)` for now, but this is a bug on our end.   

This isn't a bug, it's correct documented behavior.

```
For files with multiple images, the images will be flattened into a list
    and added to the list of available images.  In this case, ``load_func``
    should accept the keyword argument ``img_num``.
```

It is a bug in that it should not be required.  We can detect the `TypeError` and call the function without an `img_num`.

@JDWarner but those are regular .jpg, aren't they? I tend to agree with @blink1073 

> you can change your function definition to transformImage(f, img_num=None) for now, but this is a bug on our end. 

Thank you! After the fixes to transformImage(f, img_num=None) it works!


Resolving patch:
diff --git a/skimage/io/collection.py b/skimage/io/collection.py
--- a/skimage/io/collection.py
+++ b/skimage/io/collection.py
@@ -157,8 +157,7 @@ def __init__(self, load_pattern, conserve_memory=True, load_func=None,
                  **load_func_kwargs):
         """Load and manage a collection of images."""
         if isinstance(load_pattern, six.string_types):
-            load_pattern = load_pattern.replace(os.pathsep, ':')
-            load_pattern = load_pattern.split(':')
+            load_pattern = load_pattern.split(os.pathsep)
             self._files = []
             for pattern in load_pattern:
                 self._files.extend(glob(pattern))
@@ -207,7 +206,6 @@ def _find_images(self):
                     im = Image.open(fname)
                     im.seek(0)
                 except (IOError, OSError):
-                    index.append([fname, i])
                     continue
                 i = 0
                 while True:
@@ -256,10 +254,16 @@ def __getitem__(self, n):
                 if self._frame_index:
                     fname, img_num = self._frame_index[n]
                     if img_num is not None:
-                        self.data[idx] = self.load_func(fname, img_num=img_num,
-                                                        **kwargs)
-                    else:
+                        kwargs['img_num'] = img_num
+                    try:
                         self.data[idx] = self.load_func(fname, **kwargs)
+                    # Account for functions that do not accept an img_num kwarg
+                    except TypeError as e:
+                        if "unexpected keyword argument 'img_num'" in str(e):
+                            del kwargs['img_num']
+                            self.data[idx] = self.load_func(fname, **kwargs)
+                        else:
+                            raise
                 else:
                     self.data[idx] = self.load_func(self.files[n], **kwargs)
                 self._cached = n


Test patch:
diff --git a/skimage/io/tests/test_collection.py b/skimage/io/tests/test_collection.py
--- a/skimage/io/tests/test_collection.py
+++ b/skimage/io/tests/test_collection.py
@@ -80,6 +80,14 @@ def load_fn(x):
         ic = ImageCollection(load_pattern, load_func=load_fn)
         assert_equal(ic[1], (2, 'two'))
 
+    def test_custom_load_func(self):
+
+        def load_fn(x):
+            return x
+
+        ic = ImageCollection(os.pathsep.join(self.pattern), load_func=load_fn)
+        assert_equal(ic[0], self.pattern[0])
+
     def test_concatenate(self):
         array = self.images_matched.concatenate()
         expected_shape = (len(self.images_matched),) + self.images[0].shape
diff --git a/skimage/io/tests/test_multi_image.py b/skimage/io/tests/test_multi_image.py
--- a/skimage/io/tests/test_multi_image.py
+++ b/skimage/io/tests/test_multi_image.py
@@ -24,7 +24,7 @@ def setUp(self):
                      MultiImage(paths[1], conserve_memory=False),
                      ImageCollection(paths[0]),
                      ImageCollection(paths[1], conserve_memory=False),
-                     ImageCollection('%s:%s' % (paths[0], paths[1]))]
+                     ImageCollection(os.pathsep.join(paths))]
 
     def test_shapes(self):
         img = self.imgs[-1]