【问题标题】:PIL.Image.open() gives IOError: cannot identify image filePIL.Image.open() 给出 IOError: cannot identify image file
【发布时间】:2014-07-24 08:27:24
【问题描述】:

我正在尝试从 Flask 应用程序表单中保存 jpg 图像。以下代码工作正常:

blob = request.files[canvas_key]
blob.stream.seek(0)
data = blob.stream.read()
string_io = cStringIO.StringIO(data)

string_io 的类型为 <cStringIO.StringI object at 0x10bf2bf10>

但当我尝试:Image.open(string_io) 时,我收到此错误:IOError: cannot identify image file

使用stream.seek(0) 似乎可以解决other people 的这个问题,但在我的情况下它没有。

【问题讨论】:

  • 流是否有 HTTP 标头,还是真的是 JPEG?前十几个字节中是否有字符串“JFIF”?
  • 这是string_io 前缀:'RIFF\xa2_\x00\x00WEBPVP8',这是有道理的,因为我从'image/webp, and "In 2010 Google introduced the WebP picture format, which uses RIFF as a container". This is the specific line of javascript that creates the image: var dataURL = canvas.获取图像。 toDataURL('image/webp');`
  • 我认为 WebP 在 PIL 上次更新时甚至不存在。
  • 以@Marks 的评论为基础——切换到 Pillow 库。它是 PIL 的一个分支,正在更新,支持 WebP -- pypi.python.org/pypi/Pillow/2.0.0

标签: python stream python-imaging-library stringio


【解决方案1】:

感谢@Mark,我将捕获的图像类型从 WebP 更改为 PNG。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2016-04-15
    • 2013-07-23
    • 1970-01-01
    相关资源
    最近更新 更多