1

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'
Programmer S
  • 429
  • 7
  • 21
jjjjjj
  • 1,152
  • 1
  • 14
  • 30

3 Answers3

1

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.

LWixson
  • 319
  • 3
  • 13
  • Does that mean that for an example using a path, it would be `seq = imread_collection("/path/to/test/*.jpg", conserve_memory=True)`? – Programmer S May 22 '21 at 20:15
0

The documentation says:

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

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

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

  • Thanks, but no luck adding the `uri=` in front of my path. I've edited the question to better show my issue. – jjjjjj Apr 21 '18 at 18:21
  • 1
    What happens? Do you get an error? Please edit your question and include the relevant parts of your code that we need to reproduce your problem. –  Apr 21 '18 at 18:22
0

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.

FirefoxMetzger
  • 2,880
  • 1
  • 18
  • 32