Converting All Images To WEBP (With Python)

A few lines of code can save tons of space


This image is 3 MB as a PNG. It's 2 MB as a WEBP file.

WEBP (pronounced "Weppy") is a new image format released by Google in 2018. It has animation support, lossy and lossless compression, and transparency. This means that it combines the best of GIF's, PNG's, and JPEG's. Their modern compression system also allows them to save space over other formats .

The above thumbnail is a public domain 1080p picture set to 65% quality. It takes up 120 KB this way. Even if you take the same image and set it to 65% quality for a JPEG, it takes up 200 KB. WEBP may just be the best image format.

I enjoy WEBP so much, that all pictures on this site are WEBP. So, here's how to convert an image into WEBP in python:


                from PIL import Image

                input_photo = Image.open("input_path.png")
                input_photo.save("output_path.webp", method=6)
            

Basically, all we do is open a file and then save it under a different name. Of course, there is a lot more magic happening underneath the hood, but we just have to worry about the Python code. Additionally, we can actually save it under webp, png, jpeg, or anything else, so this is really an all in one converter so far.

Now this may be a bit primitave for your needs, so I built something better.

A more advanced WEBP converter

First, lets start by actually installing the required packages. We need the image converter, Pillow. Additionally, we'll be using Pillow-Heif packages to allow HEIC/HEIF (Apple image) conversion.

Installing packages

First, let's install Pillow . It's the most popular image conversion tool.

python3 -m pip install --upgrade Pillow

My original purpose for creating this conversion tool was so I could make my IPhone photos more accessible. The default format, HEIC, is way to proprietary and isn't supported by many applications. pillow-heif will allow us to convert HEIC/HEIF images to WEBP (or any other format).

pip install pillow-heif

Import headers

Now open up a python file and start writing code.

First we will add the Tkinter library so we have more flexibility for opening files


                import tkinter as tk
                from tkinter import filedialog
                import os
            

Then we can import the Pillow and HEIC libraries, and call the HEIC Register function


                #image manipulation support
                from PIL import Image
                from pillow_heif import register_heif_opener
                
                #allows us to open heic files
                register_heif_opener()
            

Gather input

Now we are going to gather some specific information from the user about how this WEBP file should be outputed.


                print ("input files:")
                input_paths = filedialog.askopenfilenames()
                print ("quality (100 for lossless):")
                pic_quality = int(input())
                loss_less = False
                if pic_quality == 100:
                    loss_less = True
                print ("output files:")
                output_path = filedialog.askdirectory()
            

First we gather the file(s), get the image quality, and then determine if lossless compression is to be used. These are all parameters that WEBP files can have.

Convert the image

Here is the final bit of the code where the image conversion takes place.


                for path in input_paths:
                    input_photo = Image.open(path)
                    file_name = os.path.basename(path).split(".")[0]
                    input_photo.save(output_path + "/" + file_name + ".webp", method=6, lossless=loss_less, quality=pic_quality)
                
            

All we do here is create an input image from a given file, convert it to a webp image, and give it all the parameters we gathered earlier.

Final code

The entire project that I created looks like this:


                #Aengus Patterson, July 2023
                #converts from heic (or any other format) to webp.
                
                #file dialogue
                import tkinter as tk
                from tkinter import filedialog
                import os
                
                #image manipulation support
                from PIL import Image
                from pillow_heif import register_heif_opener
                
                #allows us to open heic files
                register_heif_opener()
                
                print ("input files:")
                input_paths = filedialog.askopenfilenames()
                print ("quality (100 for lossless):")
                pic_quality = int(input())
                loss_less = False
                if pic_quality == 100:
                    loss_less = True
                print ("output files:")
                output_path = filedialog.askdirectory()
                print ("processing...")
                
                for path in input_paths:
                    input_photo = Image.open(path)
                    file_name = os.path.basename(path).split(".")[0]
                    input_photo.save(output_path + "/" + file_name + ".webp", method=6, lossless=loss_less, quality=pic_quality)
                
                print ("finished! hit enter to leave...")
                input()
            

What about AVIF?

In a nutshell, AVIF is a newer, even better version of WEBP. It can do all the same stuff but with better compression.

However, since it came out in 2019 and isn't backed by a huge company like Google, it lacks quite a bit of support.

I found that WEBP typically is able to get the job done, even if it's a few extra KB's more than what AVIF is promising.

I really wish I could use AVIF, but even some major browsers currently aren't supporting it well enough. Maybe in the future, I'll check it out again.

Conclusion

While this wasn't a necessarily a huge project, it does mean a lot to me. I think that if enough people pushed for it, we could make WEBP a standard in development.

WEBP is such an amazing file format, with really good compression and feature support.

That's why I built a tool to convert all of my photos to WEBP images. I hope you were able to learn something from it!