DEV Community

Pedro Valois
Pedro Valois

Posted on • Originally published at phvv.Medium on

How to Convert Images to WebP Format with Compression Using cwebp

TL;DR

To convert all images in a folder to WebP format with compression and save them in a sibling folder named webp, run the following command in the Terminal:

for file in /path/to/folder/*.{jpg,jpeg,png}; do cwebp -q 80 "$file" -o "../webp/$(basename "$file" .jpg).webp"; done
Enter fullscreen mode Exit fullscreen mode

two women sitting on sofa while using laptops, jpeg version
Two women sitting on sofa while using laptops

WebP is a modern image format that provides superior compression compared to JPEG and PNG formats. If you have a large collection of images that you want to convert to WebP format with compression, you can use the cwebp command-line tool. In this post, we’ll show you how to use the cwebp tool to convert images to WebP format with compression.

Instructions

  1. Install the WebP image format library on your system. You can download the library from the official website.
  2. Open the Terminal on your system.
  3. Navigate to the folder containing the images you want to convert using the cd command.
  4. Run the following command to convert all images in the folder to WebP format with compression and save them in the same folder:
for file in *.{jpg,jpeg,png}; do cwebp -q 80 “$file” -o “${file%.*}.webp”; done
Enter fullscreen mode Exit fullscreen mode

This command uses a for loop to iterate over all files in the folder that have a ._jpg, .jpeg, or .png extension. For each file, it runs the _cwebp command with a quality setting of 80 and saves the output file with the same original filename but with a .webp extension.

You can adjust the quality setting to a value between 0 and 100 to control the compression level. A higher quality setting results in a larger file size and better image quality.

  1. If you want to save the converted files in a different sibling folder named webp, for example, run the following command instead:
for file in *.{jpg,jpeg,png}; do cwebp -q 80 "$file" -o "../webp/$(basename "$file" .jpg).webp"; done
Enter fullscreen mode Exit fullscreen mode

This command is similar to the previous command, but it saves the output files in a sibling folder named webp with the same original filename but with a .webp extension.

Example

Suppose you have a folder named images that contains the following three image files:

images/
├── cat.jpg
├── dog.jpeg
└── bird.png
Enter fullscreen mode Exit fullscreen mode

To convert all images in the folder to WebP format with compression and save them in the same folder, follow these steps:

  1. Open the Terminal on your system.
  2. Navigate to the images folder using the cd command.
  3. Run the following command:
for file in *.{jpg,jpeg,png}; do cwebp -q 80 "$file" -o "${file%.*}.webp"; done
Enter fullscreen mode Exit fullscreen mode

After running the command, the images folder will contain the following six files:

images/
├── cat.jpg
├── cat.webp
├── dog.jpeg
├── dog.webp
├── bird.png
└── bird.webp
Enter fullscreen mode Exit fullscreen mode

The original image files are still present, and the converted files have the same original filename but with a .webp extension.

Conclusion

In this post, we showed you how to use the cwebp command-line tool to convert images to WebP format with compression. With it, you can significantly reduce the file size of your images without sacrificing image quality.

Top comments (0)