使用枕头将图像转换为webP [英] Convert images to webP using Pillow

查看:164
本文介绍了使用枕头将图像转换为webP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to convert .jpg images to webp format using PIL.

我正在使用以下代码:

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")

但是在运行它时出现以下错误:

But I get the following error on running it:

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.

Kindly help me fixing it. I have installed 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'

推荐答案

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

Make sure to install WEBP dev library for your OS. For Debian/Ubuntu it's libwebp-dev. You may need reinstall Pillow as well. In the output of Pillow install log you should see:

--- WEBP support available

这篇关于使用枕头将图像转换为webP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆