LinuxCommandLibrary

cwebp

Convert images to WebP format

TLDR

Compress a WebP file with default settings (q = 75) to the [o]utput file

$ cwebp [path/to/image_file] -o [path/to/output.webp]
copy

Compress a WebP file with the best [q]uality and largest file size
$ cwebp [path/to/image_file] -o [path/to/output.webp] -q [100]
copy

Compress a WebP file with the worst [q]uality and smallest file size
$ cwebp [path/to/image_file] -o [path/to/output.webp] -q [0]
copy

Compress a WebP file and apply resize to image
$ cwebp [path/to/image_file] -o [path/to/output.webp] -resize [width] [height]
copy

Compress a WebP file and drop alpha channel information
$ cwebp [path/to/image_file] -o [path/to/output.webp] -noalpha
copy

SYNOPSIS

cwebp [options] input_file -o output_file.webp

PARAMETERS

-o file
    Specifies the output file name. If omitted, cwebp performs compression but only reports statistics. Use "-" for standard output.

-q quality_factor
    Sets the compression quality for RGB channels between 0 (worst) and 100 (best). Default is 75 for lossy compression.

-lossless
    Encodes the image in lossless mode. This option overrides the -q option for RGB compression.

-alpha_q quality
    Sets the quality for the alpha channel (0-100). Default is 100.

-preset type
    Uses a pre-defined set of parameters for a given source type (e.g., photo, picture, icon, text). This can simplify common usage and overrides some other options.

-m method
    Specifies the compression method to use (0-6). Higher values result in slower encoding but generally better compression and smaller file sizes.

-resize width height
    Resizes the input image to the specified width and height before encoding. If either dimension is 0, it is computed maintaining the aspect ratio.

-crop x y w h
    Crops a rectangle of width w and height h from the input image, starting at coordinates x, y.

-v
    Enables verbose output, printing additional encoding information such as compression ratio, PSNR, SSIM, and encoding time.

-f strength
    Specifies the deblocking filter strength (0-100). A higher value applies more filtering, which can smooth artifacts but may blur details.

DESCRIPTION

cwebp is the official command-line tool for encoding images into the WebP format. Developed by Google, WebP is a modern image format that provides superior lossless and lossy compression for images on the web. It is designed to create smaller, richer images that make the web faster.

This utility takes various input image formats, including PNG, JPEG, TIFF, BMP, and raw YUV samples, and converts them into highly optimized WebP files. Users can control a wide range of compression parameters, such as quality factor, compression method, lossless encoding, alpha channel handling, and various pre-processing filters, to achieve the desired balance between file size and visual quality.

cwebp is an essential tool for web developers and content creators looking to optimize image delivery and improve website performance by leveraging WebP's advanced compression capabilities.

CAVEATS

The effectiveness of cwebp in reducing file size heavily depends on the source image characteristics and the chosen compression parameters. While WebP generally offers superior compression, very complex images with fine details or large areas of noise might not compress as much as simpler ones. Although WebP support is widespread across modern browsers, legacy systems or certain image applications might still lack full compatibility.

The exact set of supported input image formats (e.g., TIFF, BMP) can vary based on how the libwebp library was compiled on your system. Using high compression methods (e.g., -m 6) or processing very large images can lead to significant encoding times and memory usage.

COMMON USAGE PATTERNS

To convert a JPEG to a high-quality (80%) WebP file:
cwebp image.jpg -o image.webp -q 80

To convert a PNG to a lossless WebP file:
cwebp image.png -o image.webp -lossless

To convert and resize an image to 800x600 pixels with medium quality:
cwebp original.png -o resized.webp -resize 800 600 -q 75

To convert and discard the alpha channel for a smaller file size:
cwebp image_with_alpha.png -o noalpha.webp -noalpha -q 85

HISTORY

cwebp is an integral part of the libwebp library and its associated command-line tools, which were initially developed by Google and first released in September 2010. Its creation stemmed from Google's commitment to enhancing web performance by introducing a more efficient image format than the widely used JPEG and PNG. Since its inception, WebP, and by extension cwebp, has undergone continuous development, improving compression algorithms and features. It has gained significant traction across major web browsers and platforms, playing a crucial role in reducing web page load times and optimizing bandwidth consumption globally.

SEE ALSO

dwebp(1), webpmux(1), gif2webp(1), vwebp(1), jpegoptim(1), optipng(1)

Copied to clipboard