Search code examples
pythonpython-zipfile

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

  • 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='')