From pixels to frequencies
A photograph, stored raw, is a grid of independent brightness values with no obvious redundancy to exploit pixel by pixel. But split it into small 8x8 blocks and something useful appears: most natural image blocks are dominated by slow, smooth changes in brightness with only occasional sharp edges. The Discrete Cosine Transform re-expresses a block not as 64 pixel values but as 64 spatial frequency coefficients — how much of the block's pattern looks like a slowly varying cosine wave versus a rapidly oscillating one, in both the horizontal and vertical directions.
The DCT-II formula
JPEG uses the two-dimensional DCT-II, applied as a 1D transform along rows then columns. The 1D version for N samples is:
X_k = 2 * sum_{n=0}^{N-1} x_n * cos( pi/N * (n + 0.5) * k ), k = 0..N-1
X_0 = the DC term — proportional to the average of all x_n
X_1..X_N-1 = AC terms — increasing spatial frequency
Applied in two dimensions to an 8x8 block, this produces an 8x8 grid of coefficients where the top-left entry is the block's average brightness and coefficients further right and down represent progressively higher horizontal and vertical frequencies. Because the DCT is orthogonal and uses only real cosines (not complex exponentials like the closely related DFT), it avoids the redundant symmetric spectrum a Fourier transform would produce for real-valued image data, which is one reason it was chosen over the FFT for this job.
Energy compaction: why it works
For a typical photographic block, the DCT concentrates the overwhelming majority of the signal's energy into the low-frequency corner near the DC term, leaving most of the higher-frequency coefficients small or effectively zero. This property, called energy compaction, is the entire reason the transform is useful for compression: instead of needing to store 64 roughly equal-importance numbers, you can store a handful of large coefficients precisely and the rest coarsely — or drop them — with a visually small penalty.
Quantization: where the compression actually happens
The DCT itself is lossless (up to rounding) — it just changes representation. The lossy step is quantization: every coefficient is divided by a value from an 8x8 quantization table and rounded to the nearest integer. The table entries grow toward the high-frequency corner, because the human visual system is far less sensitive to fine high-frequency detail than to broad low-frequency shading, so those coefficients can be quantized more coarsely — often to zero — with little perceived loss. The "quality" slider in a JPEG encoder scales this table: higher quality means finer (smaller-divisor) quantization and more surviving nonzero coefficients.
Zigzag scan and entropy coding
After quantization, most of a typical block's 64 coefficients are zero, clustered in the high-frequency corner. JPEG reads the coefficients in a zigzag order that visits increasing total frequency, which turns that corner of zeros into one long run — ideal input for run-length encoding, which is then compressed further with Huffman coding. The combination of DCT, aggressive high-frequency quantization and zigzag run-length-plus-Huffman coding is what typically achieves 10:1 compression with only mild visible quality loss, and much higher ratios at the cost of visible blocking artifacts.
Frequently asked questions
Why does JPEG use 8x8 blocks instead of transforming the whole image at once?
An 8x8 block is small enough that the local image content is often fairly smooth, which concentrates energy into few coefficients, and small enough to keep the DCT computation and hardware cheap. A whole-image transform (used by wavelet-based codecs like JPEG2000) can compress better but at far higher computational cost and complexity, and it makes the characteristic block-based approach of JPEG a deliberate cost-versus-quality trade rather than an oversight.
Why does the top-left DCT coefficient matter so much more than the others?
The top-left coefficient (the DC term) is the average brightness of the whole 8x8 block — zero spatial frequency in both directions. Natural images are dominated by slowly varying regions, so this single number typically carries far more energy than any one of the other 63 AC coefficients, which is why JPEG encodes it separately and predictively from the previous block's DC value.
Why do low-quality JPEGs show visible square blocks?
Because compression is applied independently to each 8x8 block, aggressive quantization at low quality settings can leave adjacent blocks with noticeably different average brightness or color after their high-frequency detail is discarded, so the block boundaries become visible as a grid. This blocking artifact is the direct visual signature of the DCT's block-based design.
Try it live
Everything above runs in your browser — open DCT Image Compression and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open DCT Image Compression simulation