Convert PNG Images to WebP on Linux (With Commands)

The WebP image format is great at compressing photos to incredibly small file sizes. This makes it an ideal format for images on websites, just as its name would imply. Outside of web hosting, the PNG image format is much more popular and better suited to archiving.

In this tutorial, you’ll see how to convert WebP images to PNG with Linux commands. You’ll also see how to convert images into WebP, in case you plan to upload photos somewhere and want the smaller file size for your web visitors.

Install dwebp and cwebp on Linux

The dwebp command is used to convert photos from WebP to PNG. Conversely, cwebp is used to convert PNG images into WebP files.

You can install these tools with your Linux distro’s package manager by using the appropriate command below.

Ubuntu, Debian, and Linux Mint:

$ sudo apt install webp

Fedora:

$ sudo dnf install libwebp-tools

Arch Linux and Manjaro:

$ sudo pacman -S libwebp

Convert PNG images to WebP

Example 1. Use the cwebp tool to encode PNG images into WebP. All you need to do is specify the name of your PNG file, the -o (output) option, and the name of your new WebP file.

$ cwebp image.png -o image.webp

Example 2. Use the -q option to control the quality level. This can help you achieve a lower file size while sacrificing some quality in your image. A quality level of 80-85% generally gives acceptable results.

$ cwebp -q 85 image.png -o image.webp

Example 3. It’s also a good idea to use the -mt (multi-threading) option, which will better utilize your system’s CPU and convert the images more quickly.

$ cwebp -q 85 -mt image.png -o image.webp

Example 4. If you have a lot of PNG photos to convert, you can use a Bash for loop to bulk convert hundreds or thousands of PNG photos to WebP at once.

$ for f in *.png; do cwebp -q 85 -mt $f -o ${f%.*}.webp; done

Example 5. If you have PNG files scattered throughout subdirectories, you can use the find command to traverse subdirectories and convert every .PNG (or .png) file that it finds.

$ find . -iname "*.png" -exec sh -c 'cwebp -q 85 -mt "$1" -o "${1%.*}.webp"' sh {} \;

Convert WebP images to PNG

Example 1. Use the dwebp tool to decode WebP images into PNG. All you need to do is specify the name of your WebP file, the -o (output) option, and the name of your new PNG file.

$ dwebp image.webp -o image.png

Example 2. If you have a lot of WebP photos to convert, you can use a Bash for loop to bulk convert hundreds or thousands of WebP photos at once.

$ for f in *.webp; do dwebp $f -o $f.png; done

4 thoughts on “Convert PNG Images to WebP on Linux (With Commands)”

  1. There is a problem in example 5: $ find . -iname “*.png” -exec cwebp -q 85 -mt {} -o {}.webp \;

    This code create webp files but creates wrong file extension. It doesnt delete the old file extension. It generate .png.webp files

Leave a Comment

Your email address will not be published. Required fields are marked *