Converting Image Files to WebP using Python and cwebp

A handy python 3 script to batch convert those images

In this digital age, image optimization plays a crucial role in enhancing web performance. One of the currently most effective ways to optimize images is by using the WebP format, which provides superior lossless and lossy compression for images on the web. By converting your images to WebP, you can ensure faster load times and improved website performance, which in turn can help boost your SEO rankings.

This Python script, leveraging the power of the cwebp package, is a handy base to help you automate the task of converting a batch of images from various formats to WebP. Whether you have a gallery of JPEG or PNG images, you can quickly convert them all into the WebP format with a quality setting of your choice.

As long as the cwebp package is installed and Python 3 is available, this script should work.

# convert_jpg_to_webp.py
# Description: Convert all jpg files in a directory to webp

import os
import sys
import subprocess

def main():
    if len(sys.argv) != 2:
        print("Usage: python3 convert_jpg_to_webp.py <directory>")
        sys.exit(1)
    directory = sys.argv[1]
    if not os.path.isdir(directory):
        print("Directory does not exist")
        sys.exit(1)
    for filename in os.listdir(directory):
        if filename.endswith(".png"):
            subprocess.run(["cwebp", "-q", "75", os.path.join(directory, filename), "-o", os.path.join(directory, filename.replace(".png", ".webp"))])

if __name__ == "__main__":
    main()

Let's break down the key line here :

subprocess.run(["cwebp", "-q", "75", os.path.join(directory, filename), "-o", os.path.join(directory, filename.replace(".png", ".webp"))])

- "cwebp": This is the name of the command-line tool being called. The cwebp tool is used to convert images to the WebP format.
- "-q": This is an option that sets the quality of the output WebP image. The quality value can range from 0 (worst) to 100 (best).
- "75": This is the quality value being set. In this case, the quality of the output WebP images is set to 75.
- os.path.join(directory, filename): This is the path to the input image. The os.path.join() function is used to concatenate the directory path and the filename into a full path.
- "-o": This option specifies the output file.
- os.path.join(directory, filename.replace(".png", ".webp")): This is the path to the output WebP image. The .replace(".png", ".webp") part is used to change the file extension from .png to .webp.

For one image in the directory, the command interpreted will somehow look like this. This is what you would type manually.

cwebp -q 75 /home/user/images/image1.png -o /home/user/images/image1.webp

I hope you find this content useful. Don't hesitate to react or write additionnal tips or remarks in the comment section.