orbfile

Codec algorithms

How WebP Uses Prediction and Entropy Coding

See how WebP?s lossy and lossless modes use different ways to encode image data.

By the Orbfile team2 min read

WebP has two compression paths

WebP can store images with lossy or lossless compression. These modes use different techniques, so “WebP compression” does not describe one single algorithm.

Lossy WebP is based on technology from the VP8 video codec. Lossless WebP uses reversible image transforms and coding methods designed to reproduce the original pixels exactly.

Lossy WebP: predict, transform, and code

In lossy mode, the encoder divides the image into regions and predicts pixel patterns from nearby pixels. It stores the difference between the prediction and the actual image, rather than describing every pixel independently.

Those differences are transformed and quantized. Quantization reduces precision to save space, so some image information is lost. The encoder then uses entropy coding to store the remaining symbols compactly.

flowchart TD
    A[Image] --> B[Split into regions]
    B --> C[Predict from nearby pixels]
    C --> D[Calculate prediction error]
    D --> E[Transform and quantize]
    E --> F[Entropy coding]
    F --> G[Lossy WebP file]

The encoder can make tradeoffs between file size and visible detail. Fine textures and edges are useful places to inspect when comparing settings.

Lossless WebP: reversible transforms

Lossless mode must be able to rebuild the exact original pixels. It can rearrange image information using reversible transforms, such as predicting a pixel from its neighbors or changing how color values are represented. It can then use methods such as LZ77-style references, a color cache, and Huffman coding to encode repeated patterns efficiently.

flowchart TD
    A[Original pixels] --> B[Reversible image transforms]
    B --> C[Find repeated patterns]
    C --> D[Color cache and entropy coding]
    D --> E[Lossless WebP file]
    E --> F[Exact original pixels]

The encoder chooses transformations based on the image. Some images compress very well; others may not become smaller than a PNG.

What to remember when exporting

  • Choose lossy WebP when smaller files are useful and a small change in image data is acceptable.
  • Choose lossless WebP when you need exact pixel values and the receiving software supports WebP.
  • Keep transparency requirements in mind; both WebP modes can support transparency.
  • Compare the exported file with the source. Results depend on the image and encoder settings.

WebP is a format, not a guarantee of a particular file size or quality. Check compatibility with the site, app, or device that will use the image.

All guides