How are Images Compressed? [46MB ↘↘ 4.07MB] JPEG In Depth
Education
Introduction
We have an uncompressed image that uses 46 megabytes of space, and a compressed JPEG version of the same image that uses just 4.1 megabytes. Can you see the difference? What about when we zoom in to see the individual pixels? In this article, we will take a deep dive into the JPEG algorithm to see how images can be compressed to just a tenth of their uncompressed file size, all while maintaining the same image resolution and high-quality appearance.
The Importance of JPEG
Why Should You Care?
- Ubiquity: Most digital images from your phone or camera are saved using the JPEG format.
- Common Usage: An online survey showed that 86% of the images were JPEGs.
- Video Compression: Algorithms like h.264, which are used for video compression, also incorporate techniques from JPEG.
How Does JPEG Work?
JPEG analyzes each section of an image and removes elements that the human eye cannot easily perceive. When compressing an image via JPEG, you can use a sliding scale called "quality" to decide how much you want to compress the image. As the quality decreases, the file size decreases, but artifacts may become noticeable.
Step-by-Step Explanation
1. Color Space Conversion
The original image is composed of pixels that have red, green, and blue components (RGB values). Each pixel's RGB values are converted into luminance (Y), blue chrominance (Cb), and red chrominance (Cr) values.
2. Chrominance Down Sampling
Human eyes are more sensitive to luminance than chrominance. In this step, we divide blue and red chrominance component images into 2x2 blocks, find the average value for each block, and shrink the image. This removes a considerable amount of data.
3. Discrete Cosine Transform (DCT) and Quantization
These steps remove information that our eyes aren’t good at perceiving, like high-frequency elements. In DCT, we transform each 8x8 pixel block into 64 values. In quantization, we divide these values by elements in a quantization table and round them, leaving a lot of zeros in the data.
4. Run Length Encoding and Huffman Encoding
We list all the values for every block using a zigzag pattern to maximize the number of zeros. These zeros are then replaced by a simpler representation. Huffman encoding further compresses this data using a binary tree.
Rebuilding the Image
To uncompress the image, we reverse the steps: decode the Huffman encoding, multiply the values back with the quantization table, add the base images together, and finally convert the Y, Cb, and Cr values back to RGB.
Additional Insights and Caveats
- Compression Levels: You can select how much you want to compress an image, which changes the quantization table values. Too much compression leads to artifacts.
- Accuracy: Quantization reduces the precision of high-frequency data.
- Best Use Cases: JPEG is excellent for natural photography but not for vector graphics.
- Alternatives: Other image formats might offer better compression and quality.
In conclusion, the JPEG algorithm leverages human visual perception limitations to achieve impressive compression rates without noticeable loss in quality. Whether for digital photography or web images, understanding JPEG helps us appreciate the complexity behind a seemingly simple action of saving a picture.
Keywords
- JPEG
- Image Compression
- Luminance
- Chrominance
- Discrete Cosine Transform (DCT)
FAQ
How does JPEG compression work?
JPEG compression involves color space conversion, chrominance down sampling, discrete cosine transform (DCT), quantization, and encoding (run length and Huffman encoding). It removes data that the human eye cannot perceive well, thus reducing the file size.
Why is JPEG so commonly used?
JPEG is widely used because it offers a good balance between image quality and file size. It's versatile enough for most digital photography and web use cases.
What are the main steps in JPEG compression?
- Color Space Conversion: RGB to YCbCr.
- Chrominance Down Sampling: Reducing data in color channels.
- Discrete Cosine Transform (DCT) and Quantization: Transforming and compressing data blocks.
- Run Length Encoding and Huffman Encoding: Further compressing data into a manageable size.
What is chrominance down sampling?
Chrominance down sampling reduces the color information in the image. Since human eyes are less sensitive to color details, JPEG compresses the blue and red chrominance channels to save space.
What are artifacts in JPEG images?
Artifacts are visual distortions that appear in highly-compressed JPEG images, often looking like blocky or blurry areas. They occur when too much information is discarded during compression.