In this blog post, I’ll explain how JPEG achieves high compression rates by focusing on four key steps: sampling, DCT, quantization, and Huffman coding.
Why JPEG Is Widely Used
JPEG files allow photos to be stored in small file sizes with minimal distortion, making them one of the most widely used photo file formats on the internet. For example, a color image with 800,000 pixels would take up about 2 MB in BMP (uncompressed format), but a JPEG saved at approximately 80% quality is only about 200 KB—a reduction in file size of roughly 10 times. This remarkable compression rate is achieved by combining and applying the four core steps of JPEG image processing: sampling, DCT, quantization, and Huffman coding.
Sampling
In the sampling stage, the color representation is first converted from RGB (red, green, blue) to Y-Cb-Cr (or YCbCr). Y represents luminance, while Cb and Cr represent chrominance. Since the human eye is sensitive to changes in luminance but relatively insensitive to subtle changes in chrominance, reducing some of the chrominance components during storage does not result in a noticeable visual difference. By taking advantage of this, storage size can be significantly reduced by omitting some of the Cb and Cr components or lowering their resolution.
In JPEG, the Y, Cb, and Cr components can be stored at different ratios. For example, typical sampling methods such as 4:2:2 or 4:2:0 (also denoted as 2:1:1 or 1:1:1 in some literature) are used; this means that while the luminance component (Y) is maintained at its original resolution, the sampling frequency of the chrominance components (Cb, Cr) is reduced. The original text also provided an example where Y is stored once every 4 pixels, Cb once every 2 pixels, and Cr once every 1 pixel; the key point is that even when color difference components are stored less frequently, the loss in visual quality is minimal.
There are several variations of sampling methods, but in practice, they can be understood as the following three basic approaches: full sampling, in which every pixel is stored as-is; a method that skips pixels at regular intervals in both the horizontal and vertical directions; and a method that stores the average value of adjacent pixels (e.g., a 2×2 block). While these methods are often illustrated, the key point is that sampling reduces the amount of information, thereby achieving a compression effect from the very beginning.
DCT (Discrete Cosine Transform) and Quantization
After reducing the information in the chrominance components through sampling, JPEG divides the image into small blocks (typically 8×8 pixels) and applies the DCT (Discrete Cosine Transform) to each block. The DCT is a mathematical transformation that represents pixel values in the spatial domain as the sum of multiple cosine waves (frequency components). The resulting DCT coefficients place low-frequency components (large structures or smooth transitions in the image) primarily at the beginning, while high-frequency components (edges or fine textures) are placed toward the end.
To restore the compressed image, an inverse DCT (IDCT) must be applied to the DCT coefficients. It is important to note that some DCT coefficients have a significant impact on the restored image quality, while others have almost no effect. Quantization is the process of reducing the values of these less influential coefficients or setting them to zero; it is the core of lossy compression, which significantly reduces data size. Since components with little impact are discarded, this process results in a slight loss of image quality; however, in most ordinary photos, this often does not make a significant visual difference.
For example, an 8×8 block originally contains 64 pixel values, but by appropriately applying DCT and quantization, a relatively similar visual result can be achieved even if only a very small number of meaningful coefficients are retained. The example in the original text shows a case where 64 data points are compressed to 4; in this way, DCT and quantization achieve significant compression by converting spatial information into frequency components and removing less important frequency components.
Huffman Coding
Huffman coding is a step that further compresses the data obtained through sampling, DCT, and quantization. It is a lossless entropy coding method that reduces the total number of bits by assigning short codewords to high-frequency symbols and long codewords to low-frequency symbols. Since this step simply performs substitutions based on the statistical properties of the actual data, no information loss occurs here.
Let’s look at a simple example. Consider the string AAABBCAAABBAAAB; if each character is represented as an 8-bit binary number, the total is 120 bits. If we assign variable-length codes to the symbols in this data—such as 0 for A (the most frequent symbol), 100 for B, and 101 for C—we can represent the data using far fewer bits than originally required. The calculation example above shows that this can be represented in 27 bits; the key point is that coding frequently occurring symbols with shorter codes reduces the overall size.
In summary, during the sampling, DCT, and quantization stages, some information is discarded, resulting in a loss of image quality (lossy compression), while during the Huffman coding stage, the remaining data is further compressed without loss to reduce the final file size. The combination of these four stages is the secret behind the JPEG format’s ability to achieve high compression rates while maintaining relatively good image quality.
Summary
JPEG works by reducing the initial data through reduced sampling of the chrominance components, followed by lossy stages where the DCT separates the frequency components and quantization discards less important coefficients, and finally, lossless compression of the remaining data using Huffman coding. Thanks to the combination of these four stages, file sizes can be significantly reduced without any noticeable difference to the human eye.