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
4

2 回答 2

3

问题在于机器上同时存在 PIL 和 Pillow 库:

# pip freeze | grep -E '(Pillow|PIL)'
PIL==1.1.7
Pillow==2.1.0
于 2013-09-18T14:31:29.907 回答
3

我通过使用解决了这个问题

from PIL import Image

而不仅仅是做

import Image
于 2013-12-20T05:24:04.450 回答