pythonwebppython-imaging-library

Convert images to webP using Pillow


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

I'm using the this code:

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'

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'

Solution

  • 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