简体   繁体   中英

Read multiple images from same directory with imageio

I have a directory test/ with images 0.jpg , and 1.jpg . How can I use imageio 's mimread function to specify the directory test and read both 0 and 1 ? Or is this not what it's meant for?

I tried imageio.mimread(uri="/path/to/test/", format=".jpg") but got the following:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-43-89a6d166a345> in <module>()
----> 1 imageio.mimread(uri="test", format=".jpg", memtest=True)

/Users/myuser/anaconda/envs/python3/lib/python3.5/site-packages/imageio/core/functions.py in mimread(uri, format, memtest, **kwargs)
    279 
    280     # Get reader
--> 281     reader = read(uri, format, 'I', **kwargs)
    282 
    283     # Read

/Users/myuser/anaconda/envs/python3/lib/python3.5/site-packages/imageio/core/functions.py in get_reader(uri, format, mode, **kwargs)
    127 
    128     # Return its reader object
--> 129     return format.get_reader(request)
    130 
    131 

/Users/myuser/anaconda/envs/python3/lib/python3.5/site-packages/imageio/core/format.py in get_reader(self, request)
    165         if select_mode not in self.modes:
    166             raise RuntimeError('Format %s cannot read in mode %r' % 
--> 167                                (self.name, select_mode))
    168         return self.Reader(self, request)
    169 

RuntimeError: Format JPEG-PIL cannot read in mode 'I'

I've looked at the source code for imageio and I don't see any support in mimread() for iterating through the files in a folder to read in multiple images. I recommend using scikit-image instead. Here's an example:

from skimage.io import imread_collection
seq = imread_collection("*.jpg", conserve_memory=True)

Then you can index into seq to get each image:

seq[0]

seq[1]

etc.

The documentation says:

uri : {str, pathlib.Path, bytes, file}>

The resource to load the images from, eg a filename,pathlib.Path, http address or file object, see the docs for more info.

Try imageio.mimread(uri='test') .

Or is this not what it's meant for?

Correct. mimread is meant for image formats that are capable of storing more than one image in the same file. A semi-popular example of such a format is .tiff , which has a notion of pages, which may contain different images. Another one is .psd (the photoshop format).

If you want to read images from a folder, you can do it in the traditional way using a generator expression (assuming your folder only contains images you want to read

(iio.imread(filename) for filename in glob.glob(folder))

or you can be more sophisticated and write something that reads recursively and/or filters files based on your needs.

def read_folder(folder, ...):
    for item in glob.glob(folder):
        if matches_image_filter(item):
            yield iio.imread(item)
        elif matches_subfolder_filter(item):
            yield from read_folder(item, ...)

You get the idea ;)


Note: It's not the first time I hear that mimread is confusing, so if you have suggestions on how to improve clarity, feel free to leave a comment and I'll be happy to update the docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM