我写了一个小程序,在 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 图像测试了您的代码,它适用于 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 库,即 libwebp-0.4.3 并重新安装pillow。
这里是 github 问题线程,如果有人遇到同样的问题。
通过 pip install webptools
安装
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')
from PIL import Image
src = 'box.webp'
dst = 'box.jpg'
bgColor = 'white'
# Convert to RGB with Alpha channel
im = Image.open(src).convert('RGBA')
# Get Alpha channel mask
mask = im.split()[3]
# Create new RGB image with background color (transparent to bgColor)
rgb = Image.new('RGB', im.size, bgColor)
# Paste original image with mask
result = Image.composite(im, rgb, mask)
# Save result to JPG
result.save(dst, 'JPEG')
# Bad example. Save with artefacts
Image.open(src).convert('RGB').save('bad_' + dst, 'JPEG')