How the .JPG Format Works *Understanding Image Compression Through Human Perception*
Source: Dev.to
Why JPG Works So Well
The Joint Photographic Experts Group (JPG) format, introduced in 1992, is based on a simple insight:
The human eye is far more sensitive to changes in brightness than to changes in colour.
Modern camera sensors capture far more information than our eyes can perceive. JPG compression exploits this gap to dramatically reduce file size while preserving perceived image quality.
Core Steps of JPG Compression
-
Colour Space Conversion
- Images are typically stored in RGB (Red, Green, Blue).
- JPG converts RGB to YCbCr, separating brightness from colour:
- Y → Luminance (brightness)
- Cb → Blue chroma
- Cr → Red chroma
This separation lets the algorithm compress colour data more aggressively while preserving the brightness that humans notice most.
-
Block Splitting
- The image is divided into small blocks, usually 8 × 8 pixels.
- Each block can be compressed independently and at different levels based on local detail.
- Over‑compression can produce visible block artifacts.
-
Discrete Cosine Transform (DCT)
- Each block undergoes a cosine‑based transformation that converts pixel values into frequency components.
- Low frequencies → smooth areas (very important)
- High frequencies → fine detail (less noticeable)
This prepares the data for efficient reduction.
-
Quantisation (the lossy stage)
- Reduces precision, especially for high‑frequency colour data.
- Discards information the eye is unlikely to notice.
- Traverses the coefficients in a zig‑zag pattern to prioritise important values.
- This is the only truly lossy stage of JPG compression, which is why repeatedly saving a JPG degrades quality over time.
-
Entropy Encoding & Reconstruction
- The compressed data is entropy‑encoded and packed into a
.jpgfile. - On decoding, the data is converted back to RGB.
- The resulting image looks “the same” to us but contains far less data.
- The compressed data is entropy‑encoded and packed into a
Impact of Block Size & Quantisation
| Effect | Smaller Blocks | Larger Blocks |
|---|---|---|
| Detail | Higher detail, larger file | More compression, visible artifacts |
| Typical outcome | Better preservation of fine textures | Noticeable blockiness at high compression |
In practice, JPG can discard ≈ 75 % of colour detail while keeping luminance nearly intact—an excellent trade‑off for photographs.
JPG vs. PNG – A Quick Comparison
| Feature | JPG | PNG |
|---|---|---|
| Compression | Lossy | Lossless |
| Transparency | ❌ No | ✅ Yes |
| Best for | Photos | UI, logos, text |
| Typical file size | Smaller | Larger |
PNG Variants
| Variant | Colour depth | Transparency | Typical use |
|---|---|---|---|
| PNG‑8 | Limited palette (256 colours) | Single‑colour transparency | Simple graphics, icons |
| PNG‑32 | Millions of colours | True alpha (full‑range) | UI elements, high‑quality graphics |
Colour Models: Light‑Based vs. Ink‑Based
| Model | Components | Interpretation |
|---|---|---|
| RGB (light‑based) | Red, Green, Blue | Black = no light, White = full intensity |
| CMYK (ink‑based) | Cyan, Magenta, Yellow, Black | Used for printing; requires colour conversion from RGB |
Because JPG/PNG are screen‑oriented formats, they are not print‑native. Converting to a print‑ready colour space (e.g., CMYK) is necessary for professional output.
Choosing the Right Format
| Use case | Recommended format |
|---|---|
| 📸 Photography | JPG |
| 🎨 UI / Logos / Transparency | PNG |
| 🌐 Bandwidth‑sensitive apps | JPG |
| 🖨️ Professional print | Convert to a print‑native format (e.g., TIFF, PDF, or CMYK‑encoded PNG) |
Understanding how JPG works gives you real control over quality, performance, and storage—not just a slider in an export dialog.
Discussion
Have you noticed quality differences between JPG and PNG in your projects? Which format do you reach for—and why?