How Does WebP Lossless Compression Work?

WebP lossless compression reduces file sizes by combining reversible image transformations—most notably spatial prediction—with an entropy coding pipeline built on LZ77 dictionary coding and canonical Huffman encoding. Before encoding, the format transforms raw pixel values to eliminate correlation between neighboring pixels and color channels. The residual data is then compressed using a customized, backward-referencing dictionary search paired with color-indexed entropy codes, achieving significantly better compression density than traditional formats like PNG.

Spatial Prediction Transformations

Spatial prediction in WebP lossless removes spatial redundancy by estimating a pixel's color based on previously decoded neighbors (above, left, and top-left). Rather than storing raw RGBA values, the encoder records only the residual difference between the predicted value and the actual value.

WebP defines 14 distinct prediction modes evaluated on a block-by-block basis:

Because image characteristics vary across regions, the encoder divides the image into blocks (typically 16×16 pixels) and selects the optimal predictor mode for each block. These mode choices are stored in a low-resolution sub-image, which itself is compressed using entropy coding.

Additional Reversible Preprocessing Steps

Spatial prediction is one of several reversible transforms used in WebP lossless encoding:

Entropy Coding: LZ77 and Canonical Huffman Coding

After the spatial and channel transforms produce decorrelated residual values, the resulting data stream enters entropy coding. WebP adapts standard LZ77 and Huffman coding techniques with image-specific enhancements: