Support

Find answers, guides, and tutorials to supercharge your content delivery.

How WebP Compression Works

Updated on May 19, 2023
How WebP Compression Works

We've talked a lot about WebP and how it's beneficial for improving load times by reducing image sizes without sacrificing quality. However, we haven't yet gone into detail about how WebP compression actually works. There are various techniques at play in order to achieve WebP lossy and lossless compression. This post will further explain which features are available when using WebP as well as how it actually works.

Understanding the need for efficient image compression

The challenge of large image files

The digital landscape is brimming with stunning visuals, but their sheer size often poses a challenge. Large image files lead to slow page loading times, increased bandwidth consumption, and higher storage requirements. WebP comes to the rescue with its efficient compression techniques.

The birth of WebP

WebP, developed by Google in 2010, is an image format specifically designed to address these challenges. It offers impressive file size reduction without compromising image quality, making it a game-changer in the web development world.

WebP features

Like other popular image formats, WebP supports other features as well besides lossless and lossy compression, which will be further explained in the sections below. A few additional features of WebP include:

  • Animation - WebP supports animated images.
  • Transparency - The alpha channel can be used along with lossy RGB.
  • Color Profile - Can contain an embedded ICC profile.
  • Metadata - Can contain EXIF and XMP metadata.

How does WebP compression work?

WebP images can be compressed via either the lossless or lossy methods. The techniques used to achieve compression results for each method are different, which is explained in greater detail below.

Lossless WebP compression

Lossless WebP compression involves transforming images using a variety of techniques. Then, entropy coding is used on the transformed image data. The techniques used to transform images include:

  • Spatial transformation - Used to reduce entropy by taking advantage of the fact that neighboring pixels are most often correlated.
  • Color transformation - Used to decorrelate the R, G, and B values of each pixel.
  • Green subtraction transformation - Used to subtract green values from the R and B values of each separate pixel.
  • Color indexing transformation - Used to check if the number of ARGB values is below a certain threshold. If yes, an array is created of those ARGB values to replace pixel values with the corresponding index.
  • Color cache coding - Used to reconstruct new pixels from already-seen image fragments.

The techniques above are used to actually transform the image in question. Once the transformation process is complete, the last step of WebP lossless compression is to perform the entropy coding which uses a variation of LZ77 Huffman Coding called LZ77 Backward Reference.

Lossy WebP compression

WebP lossy compression uses the same predictive technology for images that is found in the VT8 video codec, more commonly known as WebM. Basically, when it comes to lossy compression, WebP considers a certain pixel's adjacent pixels and then only counts the differences as it predicts what the first pixel looks like. The compression happens through a process called the discrete cosine transform, or DCT, which converts redundant data into residual zeroes that are then removed. This process is what enables lossy files to be as small as they are.

There are additional components involved in lossy WebP compression such as macroblocking, etc. For an in-depth explanation of WebP lossy compression, consider reading more from the Google Developer's post. Additionally, the following diagram shows the steps involved in lossy WebP compression.

Source: developers.google.com

Predictive coding

At the heart of WebP lies predictive coding, a technique that exploits redundancies within the image data. By predicting pixel values based on neighboring pixels, WebP can represent images more efficiently, resulting in smaller file sizes.

Transformations and entropy coding

WebP employs a series of transformations, such as color space conversion and DCT, to further optimize the image data. These transformations help remove redundancies and compress the data more effectively. The compressed data is then encoded using entropy coding, reducing the number of bits required to represent the image.

WebP integration and browser compatibility

Encoding and decoding WebP images

To utilize the benefits of WebP compression, developers can employ encoding and decoding libraries provided by Google. These libraries facilitate the conversion of existing images to the WebP format and enable decoding for browser compatibility.

Browser support

Source: Can I use

While WebP is widely supported by major web browsers, it is essential to consider fallback mechanisms for browsers that do not support the format.

Implementing the <picture> element with appropriate source tags ensures compatibility across different browsers and provides fallback options.

Tools and resources for working with WebP

Conversion tools

Various tools are available for converting images to WebP format, including command-line tools, graphical user interfaces (GUIs), and online converters. These tools simplify the process and allow developers to optimize their images effortlessly.

WebP libraries and APIs

Google offers WebP-related libraries and APIs that developers can integrate into their applications for seamless WebP support. These resources provide functionalities such as image encoding, decoding, and manipulation, empowering developers to harness the power of WebP compression with ease.

Optimization and performance testing

Optimizing WebP images for maximum efficiency involves striking a balance between file size reduction and visual quality. Performance testing tools like Google PageSpeed Insights and WebPagetest can help assess the impact of WebP compression on website performance, allowing developers to fine-tune their implementation.

Summary

Image compression is a technical subject. As we can see, there are many techniques, methods, and technologies at play when discussing WebP image compression. Both WebP compression methods (lossy and lossless) each have their own ways of reducing file size. It is dependent upon whether you (the image owner) want to retain the same image quality and save a smaller amount of bytes (lossless) or settle for a lower quality image and save a greater number of bytes (lossy).

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more