Behind every optimized image is a sophisticated algorithm that decides what data to keep and what to discard. While you don't need to be a computer scientist to compress images effectively, understanding the basics of how these algorithms work helps you make better decisions about formats, settings, and optimization strategies. This guide explains the core concepts behind modern image compression.
The Fundamentals: What Is Image Compression?
At its simplest, image compression is the process of reducing the number of bits required to represent an image. Every digital image is essentially a grid of pixels, each with color values (typically red, green, and blue components). Without compression, a 12-megapixel photo would require about 36MB of storage—too large for efficient web delivery or storage.
Compression works by finding and exploiting patterns in this pixel data. Some patterns are easy to exploit (large areas of solid color), while others are harder (complex textures with fine detail). Different algorithms take different approaches to finding these patterns.
Lossy vs Lossless: The Fundamental Divide
All image compression algorithms fall into two categories: lossless and lossy.
Lossless compression reduces file size without removing any data. When you decompress a lossless image, you get back exactly the same pixels you started with. These algorithms work by finding redundant patterns and encoding them more efficiently. PNG and GIF use lossless compression, as does the lossless mode of WebP.
Lossy compression permanently removes some data to achieve much smaller file sizes. The goal is to discard information that the human eye is least likely to notice. JPEG and the lossy mode of WebP are lossy formats. The trade-off is that repeated compression or excessive compression will eventually produce visible artifacts.
How JPEG Compression Works
JPEG (Joint Photographic Experts Group) compression, despite being decades old, remains one of the most sophisticated and widely used algorithms. The process involves several steps:
Step 1: Color Space Conversion — JPEG converts the image from RGB (red, green, blue) to YCbCr (luminance, blue-difference chroma, red-difference chroma). The human eye is much more sensitive to changes in brightness (luminance) than to changes in color (chrominance). This separation allows JPEG to treat these components differently.
Step 2: Chroma Subsampling — Because the eye is less sensitive to color detail, JPEG can reduce the resolution of the chroma channels. Common subsampling schemes are 4:2:2 (half horizontal resolution) and 4:2:0 (half horizontal and half vertical resolution). This step alone can reduce file size by 50% with minimal visual impact.
Step 3: Discrete Cosine Transform (DCT) — The image is divided into 8×8 pixel blocks. For each block, the DCT converts spatial information (pixel values) into frequency information. The result is a grid of 64 coefficients representing different frequencies—from low frequencies (smooth areas) to high frequencies (edges and fine details).
Step 4: Quantization — This is where the actual loss happens. Each frequency coefficient is divided by a corresponding value in a quantization table and rounded to the nearest integer. High frequencies (which the eye notices less) get divided by larger numbers, effectively discarding more of that information. The quantization table determines the compression level—higher quality settings use smaller divisors.
Step 5: Encoding — The quantized coefficients are reordered using zigzag scanning (which groups similar frequencies together) and then compressed using Huffman coding (a form of entropy encoding that assigns shorter codes to frequently occurring values).
When you set a JPEG quality level, you're essentially scaling the quantization table. Lower quality = larger divisors = more data discarded = smaller file size.
How PNG Compression Works
PNG (Portable Network Graphics) uses lossless compression exclusively. Its algorithm is quite different from JPEG and is optimized for images with sharp edges, text, and flat colors.
Step 1: Filtering — PNG applies one of several filters to each scanline (row of pixels) to make the data more compressible. These filters predict pixel values based on neighboring pixels and store only the difference between the actual and predicted values. For example, if a row consists entirely of red pixels, the differences will all be zero—highly compressible.
Step 2: Deflate Compression — The filtered data is then compressed using the Deflate algorithm (the same algorithm used in ZIP files). Deflate combines LZ77 (which finds repeating patterns) and Huffman coding. It works well for images with repeated patterns and solid areas but is less effective for the complex, noisy data found in photographs.
This is why PNG excels at graphics and screenshots (which compress heavily) but produces large files for photographs (which have few repeated patterns).
How WebP Compression Works
WebP is designed to combine the strengths of JPEG and PNG while introducing modern techniques. It offers both lossy and lossless modes.
Lossy WebP uses techniques derived from the VP8 video codec. Instead of JPEG's 8×8 DCT blocks, WebP uses larger blocks and more sophisticated prediction methods. It can switch between intra-frame prediction (predicting pixels from nearby decoded pixels) and more traditional transform coding. This flexibility allows WebP to adapt better to different image content, resulting in 25-34% better compression than JPEG at equivalent quality.
Lossless WebP uses advanced techniques like entropy coding, spatial prediction, and color cache. It typically achieves 26% smaller files than PNG for the same image. The algorithm analyzes the image and selects optimal encoding methods for different regions, allowing it to handle both graphics and text efficiently.
Progressive rendering is another WebP advantage. WebP supports progressive decoding, where a low-quality version appears quickly and improves over time—improving perceived load times.
How AVIF Compression Works
AVIF (AV1 Image File Format) represents the state of the art in image compression. Based on the AV1 video codec, it uses techniques that go beyond what's possible in still-image formats:
- Multi-threading for faster encoding and decoding
- Advanced block partitioning that adapts to image content
- Multiple transform types beyond simple DCT
- Film grain synthesis that can recreate natural-looking grain without storing it
- High dynamic range (HDR) and wide color gamut support
AVIF consistently outperforms WebP by 20-30% on compression efficiency, but encoding is computationally intensive and support is still growing.
Understanding Compression Artifacts
When lossy compression is pushed too far, artifacts appear. Recognizing these helps you find the right quality balance:
JPEG artifacts: Look like blocky 8×8 grids, ringing around edges, and a general loss of fine detail. At very low quality, colors become blotchy.
WebP artifacts: Different from JPEG—WebP tends to produce smoother artifacts that can look like slight blurring or noise. It generally holds up better at lower qualities than JPEG.
PNG artifacts: As a lossless format, PNG doesn't produce compression artifacts, but improper color reduction (reducing to 256 colors) can cause banding in gradients.
Choosing the Right Algorithm
Understanding the algorithms helps you choose formats:
- Photographs: Use lossy compression. WebP offers the best efficiency, followed by JPEG.
- Graphics with text/logos: Use lossless compression. PNG is excellent; WebP lossless is better.
- Screenshots: Lossless PNG or WebP lossless. The sharp text edges require lossless quality.
- Mixed content: If an image contains both photographs and graphics (like a product photo with text overlay), WebP lossy often handles this better than JPEG.
The Future of Image Compression
As displays get higher resolution and networks get faster, the demands on image compression evolve. The trend is toward more computationally intensive encoding (which can be done offline) paired with efficient decoding (which must happen quickly on user devices). Formats like AVIF and the emerging JPEG XL are pushing the boundaries, offering better compression, HDR support, and new capabilities like lossless conversion from JPEG.
For most users today, WebP represents the sweet spot—excellent compression, universal browser support, and good encoding speed. As AVIF adoption grows, it will likely become the new default, but WebP will remain a reliable fallback for years to come.
