Pillow not loading image -cannot identify image file

徘徊边缘 提交于 2019-12-10 15:39:21

问题


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

回答1:


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



回答2:


I solved this by using

from PIL import Image

instead of just doing

import Image


来源:https://stackoverflow.com/questions/18874485/pillow-not-loading-image-cannot-identify-image-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!