【问题标题】:Error while converting webp image file to jpg in python在python中将webp图像文件转换为jpg时出错
【发布时间】:2016-01-22 15:08:54
【问题描述】:

我写了一个小程序,在python中将webp转换为jpg

import imghdr
from PIL import Image

im = Image.open("unnamed.webp").convert("RGB")
im.save("test.jpg","jpeg")

执行时出现以下错误

No handlers could be found for logger "PIL.ImageFile"
Traceback (most recent call last):
  File "webptopng.py", line 3, in <module>
    im = Image.open("unnamed.webp").convert("RGB")
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2286, in open
    % (filename if filename else fp))
IOError: cannot identify image file 'unnamed.webp'

我已经安装了带有 webp 功能的枕头。这是我的枕头安装输出

--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version      Pillow 3.0.0
platform     linux2 2.7.3 (default, Jun 22 2015, 19:33:41)
             [GCC 4.6.3]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
*** OPENJPEG (JPEG2000) support not available
--- ZLIB (PNG/ZIP) support available
*** LIBTIFF support not available
--- FREETYPE2 support available
*** LITTLECMS2 support not available
--- WEBP support available
*** WEBPMUX support not available
--------------------------------------------------------------------

请帮助我如何进行。

【问题讨论】:

  • 我从未使用过 WebP,但您的代码应该可以工作。您能否验证“unnamed.webp”实际上是一个有效的 WebP 文件,例如使用 ImageMagick 的 identifyconvert 命令?
  • 这是linux中文件命令的输出$ unnamed.webp: RIFF (little-endian) dataimagemagick的输出$ convert: no decode delegate for this image format 'unnamed.webp' @ error/constitute.c/ReadImage/532. convert: missing an image filename '/dev/null' @ error/convert.c/ConvertImageCommand/3011.
  • 抱歉,我应该提到旧版本的 ImageMagick 可能不支持 WebP。我只能建议做一个 hexdump 来检查文件的前 12 个字节是否与WebP Wikipedia article 中显示的标头匹配。并且也许尝试找到更多的 WebP 文件进行测试。
  • 文件是正确的我已经从维基百科验证了

标签: python pillow webp


【解决方案1】:

我使用 webp 图像测试了您的代码,它适用于 Pillow 2.9:

$ wget https://www.gstatic.com/webp/gallery3/2_webp_a.webp
>>> from PIL import Image
>>> im = Image.open("2_webp_a.webp").convert("RGB")
>>> im.save("test.jpg","jpeg")

Pillow 3.0 issue #1474 与您的错误相关。

让您尝试将 Pillow 从 3.0 降级到 2.9,然后重试。

【讨论】:

  • 仍然无法使用 Pillow 2.9.0 得到相同的错误 $ pip freeze | grep -i Pillow Pillow==2.9.0
  • 您是否尝试过使用相同的 webp 图像和相同的代码?与同一个枕头版本?比我建议你在 Pillow 的 github page 上打开一个问题
  • 是的,我使用了与您提到的相同的代码和相同的文件
  • @SandeepKumarSingh 我在 Pillow 的 github 页面上看到了你的 issue,问题是你有一个旧的 webp 库吗?
【解决方案2】:

这个问题现在已经解决了。我已经安装了最新的 libwebp 库,即 libwebp-0.4.3 并重新安装了枕头。

Here 是 github 问题线程,如果有人面临同样的问题。

【讨论】:

    【解决方案3】:

    通过pip install webptools安装webptools 然后:

    from webptools import dwebp
    print(dwebp("python_logo.webp","python_logo.jpg","-o"))
    

    这个库工作有点慢,但很容易做你的工作。

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 1970-01-01
      • 2018-09-10
      • 2020-10-02
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2019-06-05
      • 2017-09-10
      相关资源
      最近更新 更多