Pillow无法加载图片 - 无法识别图像文件

4
以下代码有什么问题?
它与图像格式无关,我尝试过使用jpg和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

编辑:

这种情况可能会出现在从互联网随机下载的图片及以下最基本片段中:

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

请注意,我正在使用cStringIO来重现我在第三方库django-photologue中遇到的错误。 - Enuy
2个回答

3

问题出在机器上PIL和Pillow库的相互存在:

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

3
我使用以下方法解决了这个问题:
from PIL import Image

不仅仅是执行

import Image

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接