繁体   English   中英

在 python 中将 webp 图像文件转换为 jpg 时出错

[英]Error while converting webp image file to jpg in python

我在 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 功能的枕头。 这是我的枕头安装 output

--------------------------------------------------------------------
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 图像测试了您的代码,它适用于 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 问题#1474与您的错误有关。

让我们尝试将 Pillow 从 3.0 降级到 2.9,然后再试一次。

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

如果有人遇到同样的问题, 这里是 github 问题线程。

安装由webptools pip install webptools然后:

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

这个库的工作速度有点慢,但很容易完成你的工作。

使用 webp 加载图像并继续

from PIL import Image
import webp

im = webp.load_image('test.webp').convert('RGB')
im.save('test.jpg', 'jepg')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM