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

问题描述 投票:0回答:5

我写了一个小程序,在 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
--------------------------------------------------------------------

请帮助我如何继续。

python python-imaging-library webp
5个回答
14
投票

我使用 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,然后重试。


8
投票

此问题现已解决。我已经安装了最新的 libwebp 库,即 libwebp-0.4.3 并重新安装pillow。

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


2
投票

通过 pip install webptools 安装

webptools
然后:

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

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


0
投票

使用 webp 加载图像并继续

from PIL import Image
import webp

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

-1
投票
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')

https://i.stack.imgur.com/VE3F3.png

© www.soinside.com 2019 - 2024. All rights reserved.