'Processing files with ImageIO from a ZipFile

I got an error trying to read image files from a zipfile using imageio:

import zipfile
import glob
import imageio
from os.path import splitext

    for database in glob.iglob('Datasets/*.zip'):
        print(database)
        zf = zipfile.ZipFile(database, 'r')
        for file in zf.namelist():
            basename,extension = splitext(file)
            if extension == '.png':
                img = imageio.imread(file)
                print(img.shape, end='')

Here is the traceback:

Datasets/first.zip
Traceback (most recent call last):
  File "testZip.py", line 12, in <module>
    img = imageio.imread(file)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/functions.py", line 200, in imread
    reader = read(uri, format, 'i', **kwargs)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/functions.py", line 117, in get_reader
    request = Request(uri, 'r' + mode, **kwargs)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/request.py", line 120, in __init__
    self._parse_uri(uri)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/request.py", line 252, in _parse_uri
    raise IOError("No such file: '%s'" % fn)
OSError: No such file: 'first/image_7395.png'
[Finished in 0.2s with exit code 1]


Solution 1:[1]

You're trying to open the image from the ZIP by name. imageio doesn't know how to do that. It assumes you gave it a real file path. You need to give it a file object by first opening the file in the zip. You can also extract it first like @randomdude999 suggested.

for file in zf.namelist():
    basename,extension = splitext(file)
    if extension == '.png':
        img = imageio.imread(zf.open(file))
        print(img.shape, end='')

Solution 2:[2]

The file variable contains a file name, not the actual file data. The error was pretty clear: A file named first/image_7395.png does not exist on the disk - it is in the zip though. You'll need to extract the file from the zip and use the extracted file for imageio. For example:

for database in glob.iglob('Datasets/*.zip'):
    print(database)
    zf = zipfile.ZipFile(database, 'r')
    for file in zf.namelist():
        basename,extension = splitext(file)
        if extension == '.png':
            ofile = zf.extract(file)
            img = imageio.imread(ofile)
            print(img.shape, end='')
            # if you need to:
            os.remove(ofile)

Edit: Looks like imageio can also read from a file-like object, in which case you can use ZipFile.open() and pass that to imageio. For example:

# ...
if extension == '.png':
    with zf.open(file) as img_file:
        img = imageio.read(img_file)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kichik
Solution 2