【问题标题】:Convert images to webP using Pillow使用 Pillow 将图像转换为 webP
【发布时间】:2013-11-20 13:21:07
【问题描述】:

我正在尝试使用 PIL 将 .jpg 图像转换为 webp 格式。

我正在使用此代码:

from PIL import Image
import glob, os

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile).convert("RGB")
    im.save(file + ".webp", "WEBP")

但运行时出现以下错误:

Traceback (most recent call last):
  File "webp.py", line 7, in <module>
    im.save(file + ".webp", "WEBP")
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1444, in save
    save_handler = SAVE[format.upper()] # unknown format
KeyError: 'WEBP'

请帮我修复它。我已经安装了libwebp-dev

>>> import PIL
>>> dir(PIL)
['PILLOW_VERSION', 'VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '_plugins']
>>> PIL.PILLOW_VERSION
'2.2.1'
>>> PIL.VERSION
'1.1.7'

【问题讨论】:

  • 你用的是什么版本的枕头?
  • @SamMussmann 我已经用所需信息更新了问题。

标签: python python-imaging-library webp pillow


【解决方案1】:

确保为您的操作系统安装 WEBP 开发库。对于 Debian/Ubuntu,它是 libwebp-dev。您可能还需要重新安装 Pillow。在 Pillow 安装日志的输出中,您应该看到:

--- WEBP support available

【讨论】:

  • 该解决方案对我有用,但我忘了接受答案。安装WEBP 库后重新安装Pillow 是关键。
猜你喜欢
  • 2019-11-18
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2018-05-10
  • 2022-07-07
  • 2014-09-15
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多