If you are looking to convert images to WebP free, you came to the right place. There are a lot of sites out there where you can upload and convert images, but they are polluted with tons of ads, and uploading files one by one simply doesn’t scale well. So, with this python script, you can convert images to WebP free.
Special Note: If you are looking to convert images to WebP free at scale, like, you have many thousands that you need to do all at once, you can use Bulk WebP Converter which I wrote in Go and compiled down to a standalone executable. It has concurrency and works much faster than anything I’ve been able to wrangle in Python.
Table Of Contents
What is WebP And Why Should I Use It?
Google developed WebP in 2010. However, it didn’t gain widespread usage until 2020, when Apple added support for WebP on the Desktop and Mobile versions of Safari. At this time, 93.3% of browsers support WebP by default.
If you are not using WebP for your images, you absolutely should. Image sizes will be smaller than their original file size, which means faster page loads. In addition, WebP is considered a next-generation image format, so using WebP will also positively impact your PageSpeed score. Depending on how image heavy your website is, this will also have an impact on your PageRank. Google still being the king of search in 2024, there is an obvious bias for WebP since Google created it.
Regardless of your thoughts on image formats, if you want to rank on Google, and have a better PageSpeed scrore, you need to be using WebP.
Code: Convert Folder Of JPG/JPEG, TIFF And GIF to WebP
You can adjust this example to fit your use case because many content management systems store media and image assets differently.
Using Python to do the conversion would all you to do a bunch of images at once. Some Premium WordPress plugins will handle image conversion, and if you are using one of the paid tiers of Cloudflare, you can set this to happen in-line with your traffic and not have to convert the source images at all. That being said, this method is basically free; you can run this in your DevOps pipeline, on your development workstation, or your server where your images live. You have a lot of flexibility with this script.
In this example, we will use a single directory on a computer or server to do the conversion as a large batch.
Before we cut and paste like we’re on Stack Overflow – we first need to grab Pillow, because that is what is actually doing the conversion:
$ pip install pillow
Step 1: Cut And Paste The WepP Image Conversion Script
from pathlib import Path
from PIL import Image
########################
# Lists and Variables
########################
source_image_ext = [".gif", ".png", ".jpg", ".jpeg"]
source_image_path = "source/path/here"
##############################
# 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)
Step 2: Change Source Path Of Images You Want To Convert To WebP
Name the file imageconvert.py. Change line 8 “source/path/here” to the path on your filesystem that contains the images you want to convert.
Step 3: Execute imageconvert.py To Convert Images To WebP
Run the following command from your terminal:
$ python imageconvert.py
In the directory that you specified for conversion, you will see a bunch of new .webp files. Congratulations! You just converted all your images to a nextgen image format. You can upload these files to your CMS after conversion. To convert existing images, you must re-source them in your CMS or HTML. Most CMS’s keep links to images in the database.
Optional: Output Quality Tweaks
If your WebP images are still coming out 1mb or more, you can fix that. Change line 19 to look like the following:
image.save(destination, format="webp", optimize=True, quality=10)
The quality setting ranges from 1 to 100. Quality level 1 is potato quality, and quality level 100 is the best possible quality. The lower the number, the smaller the output size will be. Depending on the nature of the source image, you might notice some image degradation, so you will need to tweak this according to your quality preferences.
Closing Thoughts
When you run this script, it leaves the source images untouched. This script is non-destructive to the source images. The only drawback to this script is it doesn’t talk to your CMS database, so you will need a way to deal with re-sourcing your images in your CMS of choice.