Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Convert Images to WebP Format in Linux?
In today's digital age, images play a vital role in websites and various digital projects. But, the larger the image size, the longer it takes to load, leading to a poor user experience. Fortunately, Google has developed the WebP image format, which offers superior compression and quality compared to traditional image formats like JPEG and PNG. In this article, we will delve into how you can convert images to the WebP format on Linux using the WebP tools. By converting your images to WebP format, you can significantly reduce their file size, which in turn boosts website performance and enhances user experience.
Step 1: Install WebP Tools
First, you need to install the WebP tools package on your Linux system. Open the terminal and run the following command
sudo apt-get install webp
The following command will generate the output like this
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: webp 0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded. Need to get 89.6 kB of archives. After this operation, 292 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 webp amd64 0.6.1-2 [89.6 kB] Fetched 89.6 kB in 1s (86.6 kB/s) Selecting previously unselected package webp. (Reading database ... 146234 files and directories currently installed.) Preparing to unpack .../webp_0.6.1-2_amd64.deb ... Unpacking webp (0.6.1-2) ... Setting up webp (0.6.1-2) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Once the installation is complete, verify that the WebP tools are installed by running the following command
cwebp -version
Here's the terminal output for the cwebp -version command
WebP Encoder version 1.1.0
This output shows the version of the cwebp command, which is used to convert images to WebP format in Linux.
Step 2: Convert Images to WebP Format
Basic Conversion Syntax
The basic syntax for converting images to WebP format is
cwebp [options] input_file -o output_file
Here's the detailed explanation of each part
cwebp This is the command to convert images to WebP format.
[options] These are optional arguments that you can use to customize the output image. The
-qoption specifies the quality of the output image.input_file This is the name of the image file that you want to convert.
-o This option specifies the output file name.
output_file This is the name you want to give the output file.
Converting a Single Image
First, navigate to the directory containing your image
cd ~/Pictures
Then convert the image using the following command
cwebp -q 80 my_image.jpg -o my_image.webp
You may get terminal output similar to this
Input file size: 654 KB Output file size: 215 KB
In this example, we've used the -q option to set the quality of the output image to 80 (out of 100). This achieves significant file size reduction while maintaining good image quality.
Step 3: Batch Conversion
When you have multiple images to convert to WebP format, you can use a loop to convert all the images in a directory at once. Here's how to do it
Navigate to the directory containing the images you want to convert, then use the following command to convert all JPEG images
for i in *.jpg; do cwebp -q 80 "$i" -o "${i%.jpg}.webp"; done
Let's break down this command
for i in *.jpgA loop that iterates over each file in the directory that has a .jpg extension.cwebp -q 80 "$i" -o "${i%.jpg}.webp"Converts each image to WebP format with quality 80.${i%.jpg}Removes the .jpg extension from the input file name.doneEnds the loop.
For PNG images, modify the command accordingly
for i in *.png; do cwebp -q 80 "$i" -o "${i%.png}.webp"; done
Common WebP Conversion Options
| Option | Description | Example |
|---|---|---|
| -q | Quality level (0-100) | -q 80 |
| -lossless | Lossless compression | -lossless |
| -resize | Resize image | -resize 800 600 |
| -crop | Crop image | -crop 100 100 400 300 |
Converting WebP Back to Other Formats
You can also convert WebP images back to other formats using the dwebp command
dwebp input.webp -o output.png
Conclusion
Converting images to WebP format is an effective way to optimize website performance by significantly reducing file sizes without compromising image quality. The cwebp command provides flexible options for both single and batch conversions, making it easy to integrate into your workflow and improve overall user experience.
