Search code examples
pythoniobufferpython-imaging-libraryzip

Python: How to read images from zip file in memory?


I have seen variations of this question, but not in this exact context. What I have is a file called 100-Test.zip which contains 100 .jpg images. I want to open this file in memory and process each file doing PIL operations. The rest of the code is already written, I just want to concentrate on getting from the zip file to the first PIL image. This is what the code looks like now from suggestions I've gathered from reading other questions, but it's not working. Can you guys take a look and help?

import zipfile
from StringIO import StringIO
from PIL import Image

imgzip = open('100-Test.zip', 'rb')
z = zipfile.ZipFile(imgzip)
data = z.read(z.namelist()[0])
dataEnc = StringIO(data)
img = Image.open(dataEnc)

print img

But I am getting this error when I run it:

 IOError: cannot identify image file <StringIO.StringIO instance at
 0x7f606ecffab8>

Alternatives: I have seen other sources saying to use this instead:

image_file = StringIO(open("test.jpg",'rb').read())
im = Image.open(image_file)

But the problem is I'm not opening a file, it's already in memory inside the data variable. I also tried using dataEnc = StringIO.read(data) but got this error:

TypeError: unbound method read() must be called with StringIO instance as 
first argument (got str instance instead)

Solution

  • Turns out the problem was there was an extra empty element in namelist() due to the images being zipped inside a direcotory insde the zip file. Here is the full code that will check for that and iterate through the 100 images.

    import zipfile
    from StringIO import StringIO
    from PIL import Image
    import imghdr
    
    imgzip = open('100-Test.zip')
    zippedImgs = zipfile.ZipFile(imgzip)
    
    for i in xrange(len(zippedImgs.namelist())):
        print "iter", i, " ",
        file_in_zip = zippedImgs.namelist()[i]
        if (".jpg" in file_in_zip or ".JPG" in file_in_zip):
            print "Found image: ", file_in_zip, " -- ",
            data = zippedImgs.read(file_in_zip)
            dataEnc = StringIO(data)
            img = Image.open(dataEnc)
            print img
        else:
            print ""
    

    Thanks guys!