Question

What's wrong with the following snippet?

It's not related to the image format, I tried both with jpg and png.

import Image
from cStringIO import StringIO

with open('/path/to/file/image.png') as f:
    data = f.read()
    img = Image.open(StringIO(data))
    img.load()

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2030, in open
     raise IOError("cannot identify image file")
IOError: cannot identify image file

EDIT:

This does happen with a randomly downloaded picture from the internet and the following most basic snippet:

import Image
im = Image.open('WicZW.jpg')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2030, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file
Was it helpful?

Solution

The problem was in the mutual presence of the PIL and Pillow library on the machine:

# pip freeze | grep -E '(Pillow|PIL)'
PIL==1.1.7
Pillow==2.1.0

OTHER TIPS

I solved this by using

from PIL import Image

instead of just doing

import Image
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top