Home/Blogs/Compression Science
Algorithm Science

How Image Compression Works: DCT, Quantization & Huffman Coding

By Crate Editorial Team•Published Oct 24, 2026•9 min read

A standard 24-megapixel photograph taken on an iPhone or Sony mirrorless camera measures 6,000×4,000 pixels. At 3 bytes per pixel (Red, Green, Blue), that uncompressed raw image data requires 72 Megabytes of storage for a single frame.

Yet when exported as a JPEG or WebP, that file shrinks to a mere 3.5 Megabytes—a 95% reduction—while looking virtually identical to the naked eye. How does this mathematical sorcery work? In this deep dive, we walk through the four stages of modern perceptual compression.

🗜️ Compress Images 100% In-Browser

Crate Compress Image optimizes JPG, PNG, and WebP files using native canvas algorithms. Inspect live side-by-side before/after sliders with zero cloud uploads.

Compress Images Now→

Stage 1: Exploiting Biology (YCbCr & Chroma Subsampling)

Human eyes contain roughly 120 million rod cells (which detect lightness and contrast) but only 6 million cone cells (which perceive color). In other words, humans are 20 times more sensitive to brightness than to color.

Compression encoders convert RGB pixels into YCbCr:

  • Y (Luma): Pure greyscale brightness information (kept at 100% full resolution).
  • Cb / Cr (Chroma): Blue-difference and red-difference color channels.

In standard 4:2:0 chroma subsampling, the encoder discards 75% of the color samples, averaging color across 2×2 pixel blocks. Your brain never even notices the missing color data.

Stage 2: Discrete Cosine Transform (DCT)

Next, the image is sliced into 8×8 pixel blocks. The Discrete Cosine Transform (DCT) converts these spatial pixel values into frequency domain coefficients.

Instead of storing 64 individual color values, DCT breaks the block down into a sum of basic mathematical sine/cosine waves: low frequencies represent smooth gradual color washes (like sky), while high frequencies represent sharp tiny edges (like grass blades).

Stage 3: Quantization (Where the Magic Happens)

Quantization is the only “lossy” step in the entire pipeline. The encoder divides high-frequency coefficients by a quality scale table and rounds them to the nearest whole integer.

Tiny high-frequency color variations that human eyes cannot perceive get rounded down to pure zeroes. At quality 85%, over 80% of the numbers in an 8×8 block become identical strings of zeroes.

Stage 4: Huffman Entropy Coding

Finally, the strings of zeroes are arranged in a zig-zag scan pattern and compressed using lossless Huffman Coding and run-length encoding (RLE). Frequent patterns are assigned tiny 2-bit symbols, while rare patterns get longer symbols, producing an ultra-compact binary file that downloads in milliseconds.

Advertisement
Google AdSense Partner SlotActive upon AdSense domain verification
Crate Creative Suite

More Free Creator Tools

100% private, client-side tools designed for video editors and creators.

Popular

Crate Captions

Auto-generate gapless 1w–5w word subtitles with smart emojis for CapCut, Premiere, & Shorts.

Video & SubtitlesLaunch →

Video Compressor

Compress MP4 and MOV videos up to 85% using in-browser WebAssembly with zero quality loss.

Video & SubtitlesLaunch →

Extract Audio

Rip clean uncompressed WAV audio from any MP4, MOV, or WebM video locally in seconds.

Audio UtilitiesLaunch →

Remove Background

Instant neural foreground isolation. Create transparent PNG cutouts without server uploads.

Image ToolsLaunch →

Convert Image

Bulk convert WebP to PNG/JPG, PNG to WebP/JPG, or JPG to WebP with one-click ZIP download.

Image ToolsLaunch →

Resize Image

Batch resize photos by pixel dimensions or percentages with aspect ratio lock and ZIP export.

Image ToolsLaunch →

Crop Image

Crop images visually with 16:9, 1:1, 9:16, and 4:5 social presets with zero quality loss.

Image ToolsLaunch →

Rotate Image

Batch rotate images 90°, 180°, or 270° and mirror flip horizontally or vertically.

Image ToolsLaunch →

Watermark Image

Batch stamp copyright text or branding across photos with 9-point grid and diagonal repeat.

Image ToolsLaunch →

Blur & Censor Image

Blur faces, pixelate sensitive personal info, or blackout credit card numbers 100% in-browser.

Image ToolsLaunch →

Meme Generator

Create viral memes with classic Impact captions, custom image upload, and zero watermarks.

Image ToolsLaunch →

Crate Drop

Stream gigabyte files directly between two devices over encrypted peer-to-peer Wi-Fi channels.

File TransferLaunch →
Popular

Font Style Changer

Transform text into 50+ aesthetic Unicode fonts for Instagram bios, TikTok captions, & Discord.

Text & TypographyLaunch →