How Does JPEG Work?
A 5-minute read
JPEG shrinks photos by converting each 8x8 block of pixels into frequency patterns and permanently discarding the detail the eye notices least.
When your phone saves a photo as a JPEG, it breaks the image into its component patterns and permanently discards the detail the eye is least likely to notice. The result looks almost identical to the original, at a fraction of the file size.
JPEG is named for the Joint Photographic Experts Group. According to jpeg.org, the JPEG 1 standard was created in 1992 as the result of a process that started in 1986. It is a lossy compression format, which means it reduces file size by permanently removing information.
The short answer
JPEG compression works by dividing an image into 8x8 pixel blocks, converting each block into its frequency patterns using a mathematical transform called DCT (Discrete Cosine Transform), and then discarding the high-frequency patterns that human eyes cannot perceive. The remaining data is quantized and encoded into a compact binary stream that decodes back into a recognizable image at a fraction of the original size.
The full picture
The color space transformation
Before JPEG touches a single pixel, it converts the image from the standard RGB color space into something called YCbCr. This separates the image into three components: brightness (Y), blue-difference (Cb), and red-difference (Cr). The human eye is far more sensitive to brightness variations than to subtle shifts in color. By keeping the brightness data intact and downsampling the color data, JPEG encoders get their first compression win before the main math begins.
This is why JPEG images degrade differently than you might expect. Loss of color information is harder to notice than loss of sharpness. A heavily compressed JPEG looks blurry and blocky, but the color relationships remain surprisingly intact.
Blocks and transforms
JPEG then divides the image into small 8x8 pixel tiles. The ITU-T T.81 specification, approved on 18 September 1992, defines these as 8 × 8 blocks of samples in its DCT-based process. This block structure is why heavy JPEG compression produces the characteristic “blockiness” visible in highly compressed images. Each block is processed independently.
For each block, the DCT converts the pixel values into a set of 64 frequency coefficients. Think of it as breaking down a messy chord into individual notes. Instead of describing each of the 64 pixels directly, DCT describes the block as a combination of patterns: a flat baseline color, a horizontal gradient, a vertical gradient, a diagonal wave, and progressively more complex checkerboard patterns.
Each of the 64 coefficients tells you how strongly a particular pattern contributes to that block. A smooth sky block has a high DC coefficient (the baseline) and near-zero coefficients for the complex patterns. A block with fine texture has high values across many of the frequency components.
Quantization: where the data dies
The quantization step is where JPEG’s deliberate information loss occurs.
After the DCT produces 64 frequency coefficients, JPEG divides each by a quantization number and rounds to the nearest integer. The quantization numbers are larger for high-frequency components than for low-frequency ones. Dividing a large number by a large number produces zero or near-zero results for most of the complex patterns.
This matters because human eyes are excellent at detecting edges and gradients but poor at detecting fine texture patterns. JPEG exploits this biological limitation by aggressively discarding the patterns we cannot easily see. The subtle texture of a brick wall, the fine grain in a cloud, the micro-variations in a patch of sky: all of this is disproportionately represented in the high-frequency coefficients, and all of it gets rounded to nothing.
A lower quality setting in your camera’s export dialog discards more of the original data. The artifacts that appear at heavy compression are the visible remnants of blocks where so much was discarded that the reconstruction looks obviously artificial.
Encoding: packing the remaining data
After quantization, most coefficients are zero. JPEG uses two encoding tricks to pack the survivors into as few bits as possible.
First, it encodes the coefficients for each block in a zigzag order, starting with the DC coefficient and moving through AC coefficients from low to high frequency. This groups the zeros together.
Second, it uses run-length encoding to describe sequences of zeros compactly, combined with Huffman coding to assign short binary codes to common patterns. The result is a binary file that bears no obvious resemblance to the original image data but contains enough information to reconstruct a close approximation.
Decompression: reconstruction without recovery
When you open a JPEG file, the decoder reverses the pipeline. It reads the Huffman codes, reconstructs the quantized DCT coefficients, multiplies by the quantization table to recover approximate coefficients, and applies the inverse DCT to convert each 8x8 block back into pixel values.
The reconstructed image is not the original. The discarded high-frequency data is gone forever. The decoder cannot know what was there. All it can do is produce a smooth approximation that satisfies the coefficients it has.
This is why re-compressing a JPEG tends to make things worse. Each encode-decode round trip can introduce new quantization artifacts, because the second compression quantizes values that were already approximations. Images that have been edited and re-saved multiple times accumulate these artifacts in a characteristic way, with edges becoming increasingly blocky and fine texture dissolving into flat color regions.
Why it matters
A 12-megapixel photo is about 36 megabytes as uncompressed 8-bit RGB. JPEG compression routinely brings this down to a few megabytes or less with minimal visible quality loss. Without compression like JPEG’s, uncompressed photos would be impractical to store and send.
But understanding JPEG’s tradeoffs matters practically. Saving a JPEG at 60% quality when 85% would have looked better is a permanent quality sacrifice. Re-exporting a compressed JPEG recovers nothing; the original data is gone. Cameras offer RAW formats because JPEG discards sensor data that would give you more flexibility in editing.
Common misconceptions
“JPEG shrinks the image resolution.” JPEG does not reduce the pixel count. A 12-megapixel photo compressed with JPEG is still a 12-megapixel photo when you open it. The pixel grid is intact. What changes is the information content of each pixel.
“Lossy means the image looks obviously worse.” At moderate-to-high quality settings, the visible difference is often hard to see under casual viewing. JPEG artifacts become obvious when you zoom in significantly or apply heavy compression.
“You can recover a JPEG to its original quality by saving at 100%.” Saving at “100%” quality reduces the quantization step but does not undo it. No JPEG encoder can reconstruct data that was discarded during the original compression. The best you can do is avoid discarding more.
Key terms
- DCT (Discrete Cosine Transform): The mathematical operation that converts each 8x8 pixel block into 64 frequency coefficients describing patterns of varying complexity
- Quantization: The step where high-frequency coefficients are divided by large numbers and rounded to zero, permanently discarding imperceptible detail
- Chrominance subsampling: The practice of storing color information at lower resolution than brightness, exploiting the human eye’s lower sensitivity to color
- Huffman coding: The entropy encoding scheme that assigns short binary codes to common patterns, packing the remaining coefficients into a compact binary stream
- Blocking artifacts: The blocky visual quality that appears in heavily compressed JPEGs, visible along 8-pixel boundaries where each block was processed independently
- Lossy compression: A compression method that permanently removes data, contrasted with lossless compression which allows exact reconstruction
Common questions
Does JPEG delete data when it saves a file?
Yes, but intentionally. JPEG's lossy compression removes detail that human eyes barely notice. Once removed, it cannot be recovered. This is why re-saving a JPEG repeatedly can cause quality to degrade over time.
What is the difference between JPEG and PNG?
JPEG uses lossy compression, which sacrifices detail for smaller file sizes. PNG uses lossless compression, meaning the original image can be reconstructed exactly. PNG also supports transparency, while JPEG does not.
Does re-saving a JPEG change its file size?
It can go either way. Each re-save runs the image through the compression pipeline again, and the second compression quantizes values that were already approximations, so quality tends to decline with each pass.
What is DCT in JPEG compression?
DCT stands for Discrete Cosine Transform. It is the mathematical heart of JPEG compression, converting each 8x8 pixel block into a set of frequency coefficients that describe patterns of light and dark across the block rather than individual pixel values.
That's the distilled version.