convert-image-webp-linux

How to Convert Images to Webp in Linux

Webp images are highly compressed quality images used for displaying images on websites & web applications. Since their file size is much smaller than JPG or PNG files, they load quickly, make your website faster and save data bandwidth. Webp is an open-source image format created by Google, and quickly getting adopted by developers and webmasters. In most cases, people simply convert their existing images to webp instead of creating them from scratch. In this article, we will learn how to convert images to webp in Linux.


How to Convert Images to Webp in Linux

We will be using webp library for our purpose. There are many webp tools & libraries available for various Linux distributions. Open terminal and run the following command to install webp tool in Ubuntu/Debian.

$ sudo apt install webp 

Once you have installed webp tool on your system, you can easily convert images to webp format using the following command. We use -q option to define output quality and -o to define output filename.

$ webp -q 60 test.png -o test.webp

You may also build the tool from source. First, download its source from Google’s repositories.

$ wget -c https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-0.6.1-linux-x86-32.tar.gz

Next, run the following commands to extract the downloaded tarball.

$ tar -xvf libwebp-0.6.1-linux-x86-32.tar.gz 
$ cd libwebp-0.6.1-linux-x86-32/
$ cd bin/
$ ls

The above ls command will display the following filenames in output. They are all meant to help you work with webp images under different circumstances.

  • anim_diff – tool to display the difference between animation images.
  • anim_dump – tool to dump the difference between animation images.
  • cwebp – webp encoder tool.
  • dwebp – webp decoder tool.
  • gif2webp – tool for converting GIF images to webp.
  • img2webp – tools for converting a sequence of images into an animated webp file.
  • vwebp – webp file viewer.
  • webpinfo – used to view info about a webp image file.
  • webpmux – webp muxing tool.

Out of all the above tools, we will use cwebp to convert an image to webp format. We use -q option to define output quality and -o to define output filename. Here is a sample command to convert image to webp format.

$ cwebp -q 60 test.png -o test.webp
OR
$ ./cwebp -q 60 test.png -o test.webp

Once you have converted the image to .webp format, you can easily view it using the vwebp viewer.

$ ./vwebp test.webp

If you want to view a list of available options for each of these tools, just run them without any option, or with -longhelp option.

$ ./cwebp -longhelp

Lastly, if you don’t want to mention full path to these commands but directly call them from anywhere in your system, add the location ~/libwebp-0.6.1-linux-x86-32/bin to your PATH environmental variable in your ~/.bashrc file.

$ vi ~/.bashrc

Add the following line at the end of the above file.

export PATH=$PATH:~/libwebp-0.6.1-linux-x86-32/bin

Save and close the file. Reload the bashrc file to apply changes.

$ source ~/.bashrc
OR
$ . ~/.bashrc

Now you should be able to run all webp tools in ~/libwebp-0.6.1-linux-x86-32/bin folder without specifying their full paths.

Here is the homepage for this library by Google.

In this article, we have learnt how to easily convert images to webp format in Linux, using webp tool. You can use this tool to convert images of other formats also, into webp. If you want to convert gif to webp then you might need to use gif2webp tool listed above.

Also read:

How to Convert Images to Webp in Python
How to Check If User Has Sudo Access
How to Combine JSON Files to CSV in Python
How to Convert JSON to CSV in Python
How to Find All Sudo Users in Linux

Leave a Reply

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