• Home
  • How To Convert An Image To WebP Using Python

This is a basic script that will convert png, jpg, jpeg and gif images in a given folder to WebP

blog-thumb

Image Conversion Using Python

We are huge fans of Python here at ActionSpec. When we optimize websites for clients, we often have to convert thousands of images to next-gen formats like WebP. We have a vast array of scripts that we wrote to do this, since every customer application is different. Despite those differences, we have a starting point that we like to use, which is the Python script we have included in this post.

Requirements To Run The Image Conversion Script

The image conversion script is pretty simple, and does not have any complex requirements.

You will need:

  • Python 3
  • Pillow
  • Pathlib

Python Image Conversion Script for WebP

Copy and paste the code below into your favorite editor, and save it as a .py file. Change lines 8 and 9 to your desired extensions and source directory.

When the script runs, it will go into the directory you point it to on line 9, and all the directories below it and process images that it finds into .webp files. It will drop the .webp image into the same directory that it found the source image that it converted. You can change this behavior in the function block of the script.

More information on hwo to use pathlib can be found in pathlib’s documentation: https://docs.python.org/3/library/pathlib.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pathlib import Path
from PIL import Image

########################
# Lists and Variables
########################

source_image_ext = [".gif", ".png", ".jpg", ".jpeg"]
source_image_path = "images/"

##############################
# FUNCTION BLOCK
##############################

# This is the function that actually does the conversion
def convert_to_webp(source):
    destination = source.with_suffix(".webp")
    image = Image.open(source)  # Open image
    image.save(destination, format="webp")  # Convert image to webp
    return destination

# This function grabs the files you want to convert and feeds them to convert_to_webp() function
def legacy_to_webp(ext, source_path):
    paths = Path(f"{source_path}").glob(f"**/*{ext}")
    for path in paths:
        webp_path = convert_to_webp(source=path)
        print(webp_path)

##############################
# EXECUTION OF LOGIC
##############################

for i in source_image_ext: 
    print(i)
    print(type(i))
    legacy_to_webp(ext=i, source_path=source_image_path)

The above code is provided without any warranty. If you would like to track the most up to date code, and look at some of the other scripts we offer to the community, have a look at our public GitHub repository.

Conclusion And Additional Resources

Python is a super flexible language for doing DevOps tasks like mass file conversions, which is why we use it for almost everything we do for our clients. Here are some resources that you might find useful if you are looking to extend the functionality of this script for your own purposes, or if you want to learn more about why image conversion is an important task in technical SEO: