April 25, 2020

How to convert JPGs to WebP automatically

The easiest way to convert a ton of JPGs to WebP is to use the cwebp command line utility written by the WebP team. Here’s how I do it using a very simple Bash script:

for f in `find ./source/uploads -name *.webp` ; do
	dir=$(dirname "$f")
    filename=$(basename -- "$f")
    extension="${filename##*.}"
    filename="${filename%.*}"
    cwebp -q 90 $f -o $dir/$filename.webp;
done

This goes through all JPG files in ./source/uploads folder, converts them with a 90% quality rate and writes the output to the same origin.