Converting Images to WebP from the Command Line

📅   ⌚ 2 minutes  

In this tutorial, learn how to convert images to WebP from the command line.

Using the libwebp library it is easy to convert .jpg, .jpeg, .png and other image file formats to WebP from the command line. Let's see how it is done.

Want to watch the video tutorial instead?

Tutorial

Please note - The library commands can be used on Windows or other supported operating systems. Not all of the commands used in the rest of tutorial are available for Windows.

Before we begin make sure that you have git installed on your system.

git --version

If you need to install git you can do so with:

sudo apt install git

Now, we need to clone the libwebp library from source.

- https://chromium.googlesource.com/webm/libwebp/

git clone https://chromium.googlesource.com/webm/libwebp

This folder should now be in your current directory.

cd libwebp

Open the README file.

cat README

Or alternatively, check it out online here.

Now make sure that you have 'make' installed.

make --version

If you need to install make you can do so with:

sudo apt install make

Or you can install the entire development package which includes make. This might be useful for you in the future, but you do not need it for this tutorial.

$ sudo apt install build-essential

Now install WebP -

sudo apt install webp

Now we are ready to install/build the library on our system. Do this while in the libwebp folder.

make -f makefile.unix

Once this is done you can delete the libwebp folder.

Now it is time to test converting a couple of images!

Convert Target to WebP with Lossy Compression

Lossy compression offers the most savings and is the default setting in the library. You can still specify settings for lossy compression with your commands.

cwebp -q 75 image.png -o image.webp

The above code explained:

  • cwebp - convert to webp
  • -q 75 - the image quality or compression level. 75 is the default - 100 would be highest quality, and 50 would be lower quality with more savings.
  • -o - specifies that you are going to give the new file a name

Convert Target to WebP with Lossless Compression

You can instead choose to use lossless compression if you wish to do so. The commands are almost identical...

cwebp -lossless image.png -o image.webp

Conclusion

Converting files to WebP in the command line is a quick and easy task once you have the libwebp library installed on your system.