Skip to content

Commit

Permalink
Merge pull request #2054 from blink1073/imagecollection-imgnum
Browse files Browse the repository at this point in the history
For imread's load_func, make the img_num argument optional
  • Loading branch information
stefanv committed Feb 21, 2017
2 parents 81a1808 + 86b8849 commit 7e9f11f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
16 changes: 10 additions & 6 deletions skimage/io/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions skimage/io/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion skimage/io/tests/test_multi_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 7e9f11f

Please sign in to comment.