Language: EN

convertir-imagenes-a-wepb-con-cwebp-cli

How to Convert Images to Webp Format

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-cli

For 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-cli

Once 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.jpg

This 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.

ParameterDescription
-f, --filesSpecifies the files to convert, separated by commas.
-r, --recursiveProcesses directories recursively.
-q, --qualitySpecifies the compression factor (between 1 and 100).
--losslessEncodes the image losslessly.
--near_losslessAdjusts the near-lossless preprocessing level. Range from 1 to 100.
-z, --zipEnables 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, --multithreadUses multiple threads for encoding if possible.
-h, --helpDisplays 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_folder

Multithreaded Compression (-mt or --multithread) If your system has multiple cores, you can speed up the conversion using the multithreading option.

cwebp -f file.jpg --multithread

Compression 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 85

Lossless Compression (--lossless): If you want to maintain the original quality without any loss, you can use the --lossless option.

cwebp -f file.png --lossless

There 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 --multithread

This 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.