使用Pillow将图像转换为webP

时间:2013-11-08 13:49:49

标签: python python-imaging-library webp pillow

我正在尝试使用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'

1 个答案:

答案 0 :(得分:19)

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

--- WEBP support available