From 0d7682773455c00ac4ea5d439d41521ed7059ad7 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 24 Oct 2014 13:24:42 -0500 Subject: [PATCH] Allow keyword arguments to imread --- skimage/io/_plugins/pil_plugin.py | 8 ++++++-- skimage/io/collection.py | 17 +++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 9c326765..b47cd3f8 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -9,7 +9,7 @@ from skimage.external.tifffile import ( imread as tif_imread, imsave as tif_imsave) -def imread(fname, dtype=None, img_num=None): +def imread(fname, dtype=None, img_num=None, **kwargs): """Load an image from file. Parameters @@ -21,6 +21,9 @@ def imread(fname, dtype=None, img_num=None): img_num : int, optional Specifies which image to read in a file with multiple images (zero-indexed). + kwargs : keyword pairs, optional + Addition keyword arguments to pass through (only applicable to Tiff + files for now. See `tifffile`'s `imread` function. Notes ----- @@ -37,8 +40,9 @@ def imread(fname, dtype=None, img_num=None): """ if hasattr(fname, 'lower') and dtype is None: + kwargs.setdefault('key', img_num) if fname.lower().endswith(('.tiff', '.tif')): - return tif_imread(fname, key=img_num) + return tif_imread(fname, **kwargs) im = Image.open(fname) try: diff --git a/skimage/io/collection.py b/skimage/io/collection.py index be1806ee..3f9e0890 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -152,7 +152,8 @@ class ImageCollection(object): """ - def __init__(self, load_pattern, conserve_memory=True, load_func=None): + 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.split(os.pathsep) @@ -180,6 +181,8 @@ class ImageCollection(object): else: self.load_func = load_func + self.load_func_kwargs = load_func_kwargs + self.data = np.empty(memory_slots, dtype=object) @property @@ -248,10 +251,11 @@ class ImageCollection(object): (self.data[idx] is None)): if self._frame_index: fname, img_num = self._frame_index[idx] - print('loading function') - self.data[idx] = self.load_func(fname, img_num=img_num) + self.data[idx] = self.load_func(fname, img_num=img_num, + **self.load_func_kwargs) else: - self.data[idx] = self.load_func(self.files[n]) + self.data[idx] = self.load_func(self.files[n], + **self.load_func_kwargs) self._cached = n return self.data[idx] @@ -399,7 +403,8 @@ class MultiImage(ImageCollection): """ - def __init__(self, filename, conserve_memory=True, dtype=None): + def __init__(self, filename, conserve_memory=True, dtype=None, + **imread_kwargs): """Load a multi-img.""" from ._io import imread @@ -409,7 +414,7 @@ class MultiImage(ImageCollection): self._filename = filename super(MultiImage, self).__init__(filename, conserve_memory, - load_func=load_func) + load_func=load_func, **imread_kwargs) @property def filename(self):