Convert images to webP using Pillow

20,112

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
Share:
20,112
Sudipta
Author by

Sudipta

The next best thing to knowing something is knowing where to find it. Interested in Machine Learning, Algorithms, Python, Django, Web Services, MySql, Mongo #SOreadytohelp

Updated on July 09, 2022

Comments

  • Sudipta
    Sudipta almost 2 years

    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'