cwebp-cli is a command-line interface (CLI) tool that allows you to convert image files in JPG and PNG formats to WebP.
WebP is a next-generation image compression format that has gained popularity in recent years due to its ability to reduce image file sizes while maintaining quality.
Cwebp-cli is a console application that is a “wrapper” around the cwebp encoder, which is part of the official WebP library developed by Google (the creator of the format).
Thanks to this tool, we can quickly and easily convert our images to WebP format from the console, without the need for additional software.
Main Features of cwebp-cli
- Support for batch conversion: cwebp-cli allows converting multiple files at once.
- Configuration: It offers various options to adjust quality, size, and compression method.
- Script compatibility: We can use it in automation scripts and integrate it into development workflows.
Installing cwebp-cli
If you only need to use the tool occasionally, you can run it directly using NPX (without the need to install it globally on your system).
npx @yukioru/cwebp-cliFor those who plan to use cwebp-cli frequently, it is recommended to install it globally on their system. This can be done, for example, using NPM:
npm install -g @yukioru/cwebp-cliOnce installed, you can run the cwebp command from anywhere in your terminal.
You can also use other package managers like PNPM or Yarn.
Using cwebp-cli
Basic Conversion
To convert a JPG or PNG image to WebP, simply run the following command:
cwebp -f file.jpgThis will generate a WebP file with the same quality and size as the original file.
Advanced Options
cwebp-cli offers a wide range of options that allow you to control specific aspects of the conversion.
| Parameter | Description |
|---|---|
-f, --files | Specifies the files to convert, separated by commas. |
-r, --recursive | Processes directories recursively. |
-q, --quality | Specifies the compression factor (between 1 and 100). |
--lossless | Encodes the image losslessly. |
--near_lossless | Adjusts the near-lossless preprocessing level. Range from 1 to 100. |
-z, --zip | Enables the lossless compression mode with a level from 0 to 9. |
--resize <width/height> | Resizes the image. |
--crop <x_position/y_position/width/height> | Crops the image. |
-mt, --multithread | Uses multiple threads for encoding if possible. |
-h, --help | Displays help for the command. |
Recursive Conversion (-r or --recursive) This option allows you to traverse a complete directory and convert all JPG and PNG images to WebP.
cwebp -r ./image_folderMultithreaded Compression (-mt or --multithread) If your system has multiple cores, you can speed up the conversion using the multithreading option.
cwebp -f file.jpg --multithreadCompression Quality (-q or --quality)
You can specify a value between 0 and 100 to adjust the quality of the WebP image. A lower value reduces the file size but also the quality. The default value is 75.
cwebp -f file.jpg -q 85Lossless Compression (--lossless): If you want to maintain the original quality without any loss, you can use the --lossless option.
cwebp -f file.png --losslessThere are many more options available. Check the official documentation for more information.
Complete Usage Example
Imagine you have a directory full of JPG and PNG images that you want to convert to WebP, maintaining high quality and using multithreaded compression.
The command would be:
cwebp -r ./my_directory --quality 70 --multithreadThis command will traverse the my_directory, convert all images to WebP with a quality of 90, and use multiple threads to speed up the process.
Cwebp-cli is Open Source, and all the code and documentation is available in the project’s repository on GitHub - Yukioru/cwebp-cli
