brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cwebp -q 80 converts a PNG or JPEG to a smaller WebP file

Tools
~/tips/tip-images-cwebp-convert-to-webp

cwebp -q 80 photo.jpg -o photo.webp

Converts a PNG, JPEG or TIFF image to WebP, with a quality setting from 0 to 100.

WebP files are usually smaller than the JPEG or PNG they came from, and every current browser opens them. Google’s libwebp package has a small converter for it.

cwebp -q 80 photo.jpg -o photo.webp
cwebp -lossless logo.png -o logo.webp

According to the cwebp documentation, -q sets the compression factor between 0 and 100 and the default is 75. Lower numbers give smaller files with less detail. The input can be PNG, JPEG, TIFF or WebP. The output name goes after -o, and if you leave -o out, cwebp only prints statistics and writes no file at all, which confuses people the first time.

For graphics with sharp edges, such as logos and screenshots, -lossless encodes without any loss. Transparent pixels only keep their hidden colour values when you add -exact.

A resize in the same step works too: -resize 1200 0 sets the width and calculates the height, since the documentation says a 0 for one side keeps the aspect ratio.

To convert a whole folder from the shell:

for f in *.jpg; do cwebp -q 80 "$f" -o "${f%.jpg}.webp"; done

The caveat is that cwebp does not read animated GIF files, and it does not take animated PNG or WebP either. For those you need other tools from the libwebp set. Keep the originals as well, because converting a JPEG to lossy WebP loses a bit of quality again.

Further reading: Google: cwebp documentation

More Tools tips shuf -n 3 films.txt xmllint --format feed.xml pdftotext -layout invoice.pdf invoice.txt qrencode -t ANSIUTF8 'https://example.com/a/long/path?id=12345' where python All Tools tips (14) All topics

Link