Convert WebP images to PNG in Linux and BSD via the command line

Dec 28, 2020 · 1 min read
Convert WebP images to PNG in Linux and BSD via the command line
Share this

WebP is the updated web image format created by Google, primarily intended to leave a lighter footprint for websites. Impressively, they were able to create a new image format while [roviding a high compression ratio, leaving the original image quality in tact.

Having said that, it can be quite annoying to come accross a webp image and trying to upload directly to an image host that doesn’t support it (like Imgur). Luckily, there are a couple command line programs that one could easily create bash script for bulk conversion.

FFMPEG

You probably have ffmpeg installed on your Linux machine, and if not, it’s available in just about any repository.

This simple command will produce a PNG image:

ffmpeg -i image1.webp image2.png

You may get a slew of error messages depending on your version, but it does indeed convert.

Here’s an example of a bash script to help automate the process:

#!/bin/bash
for a in ./*.webp; do
  old=$IFS
  IFS="\n"
  ffmpeg -i "$a" "${a[@]/%webp/png}"
  IFS=$old
done

Libwebp

While ffmpeg is great, it creates larger-than-desired images if you’re looking to convert & store high quantities of images. This is where the native libwebp would be preferred.

For Debian-based distros, install via:

# apt-get install webp

For Arch-based distros:

# pacman -S libwebp

While this will make several new commands available, the command of interest is dwebp. Simply issue this line in a terminal window:

dwebp image.webp -o image.png

And to do the reverse process, use cwebp:

cwebp -q 80 file.png -o file.webp

Sponsors

Best VPN
Join Newsletter
Get the latest post right in your inbox.