diff --git a/scikits/image/io/__init__.py b/scikits/image/io/__init__.py index 0d3f4c42..bb64a817 100644 --- a/scikits/image/io/__init__.py +++ b/scikits/image/io/__init__.py @@ -1,3 +1,4 @@ from pil_imread import * from sift import * +from io import * diff --git a/scikits/image/io/io.py b/scikits/image/io/io.py index deafaa66..d8d9925c 100644 --- a/scikits/image/io/io.py +++ b/scikits/image/io/io.py @@ -2,7 +2,7 @@ from __future__ import with_statement -__all__ = ['MultiImage', 'ImgCollection', 'imread'] +__all__ = ['MultiImage', 'ImageCollection', 'imread'] from glob import glob import os.path @@ -114,7 +114,7 @@ class MultiImage(object): """ numframes = self._numframes if -numframes <= n < numframes: - n = n% numframes + n = n % numframes else: raise IndexError, "There are only %s frames in the image"%numframes @@ -143,24 +143,35 @@ class MultiImage(object): class ImageCollection(object): """Load and manage a collection of images.""" - def __init__(self, file_pattern, conserve_memory=True, grey=False): + def __init__(self, file_pattern, conserve_memory=True, as_grey=False): """Load image files. Note that files are always stored in alphabetical order. - Input: - ------ - file_pattern : string - Path and pattern of files to load, e.g. ``data/*.jpg``. - conserve_memory : bool + Parameters + ---------- + file_pattern : str or list of str + Path(s) and pattern(s) of files to load. The path can be absolute + or relative. If given as a list of strings, each string in the list + is a separate pattern. Files are found by passing the pattern(s) to + the ``glob.glob`` function. + conserve_memory : bool, optional If True, never keep more than one in memory at a specific time. Otherwise, images will be cached once they are loaded. - grey : bool - If True, convert the input images to grey-scale. + as_grey : bool, optional + If True, convert the input images to grey-scale. This does not + affect images that are already in a grey-scale format. - Example: + Attributes + ---------- + files : list of str + A list of files in the collection, ordered alphabetically. + as_grey : bool + Whether images are converted to grey-scale. + + Examples -------- - >>> from os.path import dirname, join + >>> from os.path import dirname, join #doctest: +SKIP >>> data_dir = join(dirname(__file__), 'tests') >>> c = ImageCollection(data_dir + '/*.png') @@ -168,47 +179,55 @@ class ImageCollection(object): 3 >>> c[2].shape (20, 20, 3) - """ - self.files = sorted(glob(file_pattern)) + if isinstance(file_pattern, basestring): + self._files = sorted(glob(file_pattern)) + elif isinstance(file_pattern, list): + self._files = [] + for pattern in file_pattern: + self._files.extend(glob(pattern)) + self._files.sort() if conserve_memory: memory_slots = 1 else: - memory_slots = len(self.files) + memory_slots = len(self._files) self.conserve_memory = conserve_memory - self.grey = grey + self._cached = None + #TODO: make as_grey a property, or provide a setter that reloads data + self.as_grey = as_grey self.data = np.empty(memory_slots, dtype=object) - def __getitem__(self, n, _cached=np.array(-1)): - """Return image n in the queue. + @property + def files(self): + return self._files + + def __getitem__(self, n): + """Return image n in the collection. Loading is done on demand. - Input: - ------ + Parameters + ---------- n : int - Number of image required. + The image number to be returned. - Output: + Returns ------- - img : array - Image #n in the collection. - + img : ndarray + The `n`-th image in the collection. """ + num = len(self.files) + if -num <= n < num: + n = n % num + else: + raise IndexError, "There are only %s images in the collection"%num idx = n % len(self.data) - if (_cached != n and self.conserve_memory) or (self.data[idx] is None): - image_data = imread(self.files[n], self.grey) - #with file(self.files[n]) as f: - #exif = EXIF.process_file(f) - - #self.data[idx] = Img(image_data, - #filename=os.path.basename(self.files[n]), - #EXIF=exif, info={}) - - _cached.flat = n + print num, idx, n + if (self.conserve_memory and n != self._cached) or (self.data[idx] is None): + self.data[idx] = imread(self.files[n], self.as_grey) return self.data[idx]