枕头未加载图像-无法识别图像fi

2024-05-16 11:00:29 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的片段有什么问题?

它与图像格式无关,我尝试了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

编辑:

从internet随机下载的图片和以下最基本的片段确实会出现这种情况:

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

Tags: inimageimportdatapnglineopenfile