How to read an image inside a zip file with PIL/Pillow

Tor Klingberg picture Tor Klingberg · Oct 16, 2015 · Viewed 10.1k times · Source

Can I open an image inside a zip with PIL/Pillow without extracting it to disk first?

Answer

jfs picture jfs · Oct 16, 2015

The recent Pillow releases do not require .seek():

#!/usr/bin/env python
import sys
from zipfile import ZipFile
from PIL import Image # $ pip install pillow

filename = sys.argv[1]
with ZipFile(filename) as archive:
    for entry in archive.infolist():
        with archive.open(entry) as file:
            img = Image.open(file)
            print(img.size, img.mode, len(img.getdata()))