Installation
In order to create or convert WebP from the command line, you must install appropriate packages first. The process is straightforward in most cases.
Ubuntu/Debian
apt install webp
RHEL/Centos
yum -y install libwebp-tools
MacOS
brew install webp
This will install the utilities needed to create images in a WebP format and convert WebP to different formats - cwebp, dwebp, gif2webp, img2webp, webpinfo. In this tutorial we’ll focus on two programs - cwebp and dwebp.
Creating WebP
To create WebP use cwebp utility. It compresses input image to WebP. Note that it doesn’t work with animated images.
The basic syntax is as follows:
cwebp input_image.png -o output_image.webp
Supported input formats are WebP (yes, you can recompress a WebP with this tool), JPEG, PNG, PNM (PGM, PPM, PAM), TIFF.
Other basic options include -q
which change the quality level - 0 is minimum, and 100 is maximum quality. Another interesting option is to use -lossless
, interestingly in some cases - especially on images with simple geometric shapes it can actually produce smaller and better-looking images. I highly recommend messing around with quality settings to get the desired effect.
cwebp can also resize and crop images. Using resize -resize
option is pretty straightforward. I you use this flag you have to place two numbers width and height. To preserve the original aspect ratio you can set one of the params to 0. Cropping is a little bit more involved. The syntax is the following -crop <x> <y> <w> <h>
- as you can see you have to manually enter the desired crop dimensions and position.
Quite a few other options are available, to get the reference just use -longhelp
option in your command line.
Converting from webp
To convert webp to another format use dwebp tool.
Basic usage:
dwebp input_image.webp -o output_image.png
Note you can change the extension of the file as you please, but it won’t change the actual image format. By default, it would be a png. You can change the format by using the -bmp
, -tiff
, -pam
, -ppm
, -pgm
, or -yuv
options.
Hope this helps you to introduce this high-compression format to your workflow… or helps you convert WebP memes your buddies sent you over slack to png. In my case, I learned about it, to use it on this website - as I’m cheap and don’t want to pay for external image processing.