10.5 years ago Google released revolutionary image format WebP, dubbed by many “a game changer for #SEO“. It offers up to 80% reduction of image size with minimal impact on image quality.
Since 2020, WebP is supported by all modern web browsers. The only problem is, is it’s a pain to convert mass amounts of images into this new format. Fear not: this little Python script I wrote helps take care of that. It scans your images directory and all of its sub-directories for any images and converts them to WebP blazingly fast, so that you don’t have to kill your server with slow, resource-heavy expensive plugins — or waste time on lame online tools that charge outrageous fees.
Here it is:
First, be sure to pip install pillow:
From the Command Line or Terminal, type: pip install pillow
Then, execute the following:
import os, PIL
from PIL import Image
MY_DIR = “.” // place your path to your image directory
for root, dirs, files in os.walk(MY_DIR):
for _this_file in files:
_source = root + os.sep + _this_file
im = Image.open(_source).convert(“RGB”)
im.save(_source.rsplit(‘.’, 1) + “.webp”, “webp”)