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

How to Convert Images to Webp Format

  • 4 min

cwebp-cli is a command-line tool (CLI) that allows you to convert image files from 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 acts as 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

  • Bulk Conversion Support: cwebp-cli allows converting multiple files at once.
  • Configuration: Offers various options to adjust quality, size, compression method.
  • Script Compatibility: Can be used in automation scripts and integrated 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 archivo.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, --zipActivates 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, --helpShows help for the command.

Recursive Conversion (-r or --recursive) This option allows you to traverse an entire directory and convert all JPG and PNG images to WebP.

cwebp -r ./carpeta_imagenes

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

cwebp -f archivo.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 file size but also quality. The default value is 75.

cwebp -f archivo.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 archivo.png —lossless

There are many more options available. Consult 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 ./mi_directorio —quality 70 —multithread

This command will traverse the my_directory directory, convert all images to WebP with a quality of 90, and use multiple threads to speed up the process.