hakk

software development, devops, and other drivel
Tree lined path

Convert Images to WebP using the Linux command line and Docker

The webp command line tool from Google makes it quite simple to convert images to the webp format from the command line. It can convert JPG, PNG, and TIFF images to WebP. Not sure what webp is? Read more about webp on Wikipedia.

If you don’t have Docker installed on your system already, go ahead and install it now.

Spin up a Debian Bookworm docker container and mount the directory with your images to the container.

docker run -it --rm -v /path/to/local/images:/images debian:bookworm bash

Next update the apt repositories and install wget.

apt update && apt install wget -y

Then head over to the webp utility websit and get the link for the precompiled command line tool.

# move into the /tmp directory
cd /tmp
# download the tool
wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.3.2-linux-x86-64.tar.gz
# extract the tool
tar -xvf libwebp-1.3.2-linux-x86-64.tar.gz 
# add the tool to the path
export PATH=$PATH:$(pwd)/libwebp-1.3.2-linux-x86-64/bin

Now that the tool is available let’s head over to the images directory and convert the images to webp.

cd /images

Many parameters can be supplied to the utility. Check out the webp website or cwebp --help for more information.

cwebp -q 50 images/cat.jpg -o images/cat.webp

Check out this script if you need to bulk convert images.