Image Compression

A closer look at popular image formats and their compression techniques: JPEG and GIF.

Introduction to Image Compression

In our digital world, images are everywhere. From family photos stored on our phones to the graphics that make up websites, digital images are a fundamental part of how we communicate and store memories. However, raw, uncompressed images are incredibly large. A single high-resolution photograph can take up tens of megabytes of space. Storing thousands of such photos would quickly fill up any hard drive, and sending just one over a slow internet connection could take minutes.

This is where image compression comes in. is the art and science of making image files smaller, enabling us to store more images and share them more quickly. The core idea behind all compression is to identify and reduce within the image data. This can be repetitive patterns, large areas of the same color, or details that the human eye is unlikely to notice. The techniques used to achieve this fall into two primary categories: Lossless and Lossy compression.

The Fundamental Divide: Lossless vs. Lossy Compression

The choice between lossless and lossy compression is the most critical decision in handling digital images, as it defines the trade-off between file size and perfect fidelity to the original. Each approach serves a different purpose and is suited for different types of images.

Lossless Compression: Perfect Reconstruction

is like a perfect vacuum sealer for data. It squeezes out all the unnecessary space without altering the contents. When you decompress a losslessly compressed file, you get back the exact, identical data you started with, bit for bit. No information is ever lost.

This is achieved by finding statistical redundancy in the data. For example, instead of storing "blue, blue, blue, blue, blue," a lossless algorithm might store "(blue, 5 times)". Another technique involves creating a dictionary of common patterns and replacing them with shorter codes. Consider this famous line of text:

To be, or not to be, that is the question

A lossless algorithm might notice the phrase "to be" appears twice and replace the second instance with a short pointer that says "refer to the first 5 characters." This saves space without changing the meaning. The amount of compression achievable depends heavily on the data itself; highly repetitive data compresses well, while random data compresses poorly.

When to use it: Lossless compression is essential when every single bit of data is critical and cannot be altered. Applications include text documents, computer programs, medical imaging (like MRI scans), and technical diagrams where precision is paramount. Common lossless image formats include PNG and GIF.

Lossy Compression: Intelligent Approximation

takes a more aggressive approach. It achieves significantly smaller file sizes by permanently throwing away data. The key is that it tries to discard information that our human senses are least likely to miss.

This method relies on : scientific understandings of how the human eye and brain perceive the world. For instance, we are much more sensitive to changes in brightness than we are to subtle variations in color. A lossy algorithm might exploit this by storing brightness information with high precision while storing color information more crudely. The result is a file that is not a perfect copy, but is hopefully "perceptually lossless," meaning it looks the same to a human observer. The degree of compression is adjustable via a "quality" setting, allowing for a trade-off between file size and visual fidelity.

When to use it: Lossy compression is ideal for photographic images, video, and audio, where perfect bit-for-bit accuracy is not required and a slight loss of quality is an acceptable price for a massive reduction in file size. The most famous lossy image format is JPEG.

Important Warning: The data removed during lossy compression is gone forever. Repeatedly saving a file in a lossy format (e.g., editing a JPEG and saving it again as a JPEG) will cause quality to degrade with each save, a phenomenon known as .

GIF Format: The Power of the Palette

GIF, which stands for Graphics Interchange Format, is one of the oldest and most enduring image formats on the web. Its longevity is due to its simplicity and its unique features, primarily its support for animation. GIF compression is a two-step process that cleverly combines both lossy and lossless techniques.

Step 1: Indexed Color Palette (The Lossy Part)

The defining feature of a GIF is its use of an indexed color palette. Instead of storing the full color value (typically 24 bits, representing over 16 million colors) for every single pixel, a GIF file first creates a small, custom lookup table of colors called a palette. This palette can contain a maximum of 256 colors.

This is where the initial, and most significant, compression (and loss) occurs. If the original image contains thousands or millions of colors (like a photograph), the GIF encoder must perform . It analyzes the original image and selects the "best" 256 colors to represent it. All other colors in the original image are then mapped to their closest match within this new limited palette. This step is inherently lossy, as subtle gradients and shades are lost. However, for images that already have few colors, like logos or diagrams, this step can be virtually lossless.

Once the palette is established, each pixel in the image is no longer represented by its full color value, but by a simple 8-bit number (28=2562^8 = 256) that serves as an index to its color in the palette.

Step 2: LZW Compression (The Lossless Part)

After the image has been converted to a stream of color indices, a true lossless compression algorithm is applied. GIF uses the Lempel-Ziv-Welch (LZW) algorithm. LZW is a dictionary-based compression method. It scans the stream of pixel indices and builds a dictionary of recurring sequences. When it encounters a sequence it has seen before, it outputs a short code pointing to that sequence's entry in the dictionary, instead of the sequence itself. This is very effective for images with large areas of flat color, as it can represent a long run of identical indices with just a few codes.

Special Features and Use Cases

  • Animation: GIF's most famous feature is its ability to store multiple frames within a single file, along with timing information, creating simple, looping animations that are ubiquitous on the internet.
  • Transparency: GIF supports basic binary transparency. One color in the palette can be designated as transparent, allowing the background of a webpage to show through. It does not support partial transparency (alpha channels).
  • Interlacing: An interlaced GIF loads in several passes. A low-resolution version appears quickly and gradually becomes sharper as more data is downloaded, improving the perceived loading speed.

GIF palette explorer

Reduce the number of available colours to see banding and posterisation appear in a synthetic scene.

Colours
256
Bits per pixel
8 bpp
Estimated size
1.60 MB
Banding
Smooth
Indexed preview256

Pixels are uniformly quantised and dithered with a 4x4 Bayer matrix to mimic a GIF encoder.

Sample palette+232

Only the first 24 shades are shown for large palettes.

JPEG Format: Mastering Perception for Photographs

JPEG (named after its creator, the Joint Photographic Experts Group) is the world's most popular lossy compression format, and for good reason. It is specifically designed to compress continuous-tone images like photographs, where subtle changes in color and brightness are common. The JPEG algorithm is a masterful application of psycho-visual principles to achieve very high compression ratios while maintaining excellent visual quality.

The JPEG compression process can be understood as a multi-step pipeline:

  1. Color Space Transformation:

    The image is typically converted from the RGB (Red, Green, Blue) color space to a luminance/chrominance space called YCbCr. This separates the brightness information (Y, or Luma) from the color information (Cb and Cr, or Chroma). This is done because the human eye is far more sensitive to fine details in brightness than it is to fine details in color.

  2. Chroma Subsampling (Downsampling):

    Leveraging this aspect of human vision, the color (chroma) components are downsampled. This means their resolution is reduced, effectively storing color information for blocks of pixels rather than for every single pixel. A common scheme is 4:2:04:2:0, where a 222 \times 2 block of pixels shares the same Cb and Cr values while each retains its individual Y value. This step alone can reduce the file size significantly with little to no visible impact.

  3. Discrete Cosine Transform (DCT):

    The image is divided into small 888 \times 8 pixel blocks. Each block is then processed by the . The DCT doesn't change the pixel values themselves, but rather represents them in a different way, as a combination of 64 frequency coefficients. The first coefficient, the DC coefficient, represents the average brightness/color of the block. The other 63 AC coefficients represent the details and textures within the block, from gentle gradients to sharp edges.

  4. Quantization:

    This is the main lossy step and the heart of JPEG's compression. Each of the 64 DCT coefficients is divided by a corresponding value from a 64-element , and the result is rounded to the nearest integer. The values in this table are carefully chosen: small numbers are used for the important low-frequency coefficients (like the average color), preserving them with high precision. Much larger numbers are used for the high-frequency coefficients, which represent fine details. This aggressive division and rounding causes most of the high-frequency coefficients to become zero, effectively discarding the information about the finest details that the eye is least likely to miss. The "quality" setting of a JPEG file directly adjusts the values in this table.

  5. Entropy Coding (Lossless):

    In the final stage, the resulting quantized coefficients are compressed using a lossless algorithm. The coefficients are first read out in a zig-zag pattern to group the many zero-valued coefficients together. This long run of zeros is then efficiently compressed using (RLE). Finally, the entire stream of symbols is compressed using Huffman coding to achieve the final, small JPEG file.

JPEG quality explorer

Move the slider to see how aggressive quantisation shrinks the file and introduces familiar artifacts.

Approx. ratio
2.40:1
Estimated size
3.13 MB
Estimated PSNR
42.4 dB
Artifacts
Light blocking
Sample frameQ80

Simulation downsamples the scene, emphasises 8x8 block boundaries, and adds mild ringing to mimic JPEG artifacts.

Quality vs compression2.40:1
Approx. ratioQuality setting

Curve shows the estimated compression ratio for a typical 12 MP photo saved as JPEG.

    Image Compression