How does the Discrete Cosine Transform (DCT) work in JPEG?
General knowledgeIn depthAsked by Anonymous from Seattle, United States23 Oct 2026, 10:50 PM ISTUpdated
In one line
The Discrete Cosine Transform (DCT) turns parts of an image into numbers that show how much of each pattern is present, making it easier to compress and store images like JPEGs.
JPEG — Lossy compression method for reducing the size of digital images JPEG images use DCT to compress photos, like the ones you take on your phone. Image: Wikipedia
The rule
The Discrete Cosine Transform (DCT) converts a signal or image from the spatial domain (where we see pixels) to the frequency domain (where we see patterns of change), allowing us to separate important details from less important ones for compression.
There is a formula for the 1D DCT:
X_k = Σ (from n=0 to N-1) x_n × cos[π/N × (n + 0.5) × k] for k = 0 to N-1
X_k: DCT coefficient (the strength of the k-th frequency pattern)
x_n: the original value (like a pixel)
N: total number of values (like pixels in a row)
k: which frequency we are measuring
JPEG uses the 2D version, which works on blocks of pixels.
The formula
X_k = Σ x_n × cos[π/N × (n + 0.5) × k]
X_kDCT coefficient for frequency k
x_noriginal value (pixel)
Nnumber of values
kfrequency index
Let's understand it simply
Imagine you are rolling out a big roti (chapati). Some parts are thick, some are thin. If you want to describe the roti to a friend, you could say, "It is mostly flat, but there are some waves and bumps here and there."
The DCT does something similar for images. It looks at a small block of the image (usually 8×8 pixels) and says, "This block is mostly smooth, but there are some stripes, some edges, and some fine details." It turns the block into a list of numbers, each showing how strong a certain pattern (like a stripe or checkerboard) is in that block.
How it works
Divide the image into 8×8 blocks: The JPEG process cuts the image into small squares, each 8 pixels by 8 pixels.
Shift pixel values: Each pixel value (0–255) is shifted down by 128, so they range from -128 to +127. This helps with math later.
Apply the DCT: For each 8×8 block, the DCT formula is used. It turns the 64 pixel values into 64 DCT coefficients (numbers).
Most energy in a few coefficients: The top-left number (called the DC coefficient) shows the average color. The rest show patterns: stripes, edges, and tiny details. Usually, most numbers are close to zero except a few.
Quantization: The DCT numbers are divided by standard values (quantization table), and then rounded off. This step throws away tiny details the human eye can't see, making the image smaller in size.
Store only important numbers: Many of the DCT numbers become zero after quantization. JPEG stores only the non-zero numbers efficiently.
How DCT works in JPEG
Divide image into 8×8 blocks
Shift pixel values down by 128
Apply DCT to each block
Quantize (round off) DCT numbers
Store only important numbers
The story behind it
The DCT was first described by Nasir Ahmed, T. Natarajan, and K. R. Rao in 1974. They were looking for a way to compress images and signals so they could be sent over phone lines or stored using less space. The big idea was to find a way to keep the important parts of an image and throw away the rest, without making the picture look bad. Ahmed came up with the DCT while working as a professor in the US. Today, almost every JPEG photo uses this method.
Where you see it in real life
Photos on your phone: When you take a picture, your phone uses JPEG compression. DCT helps shrink the file size.
WhatsApp images: When you send a photo on WhatsApp, it is compressed using JPEG (and DCT), so it sends faster.
Websites: Most images you see online (like on shopping sites) are JPEGs, compressed with DCT.
Digital cameras: Every digital camera uses JPEG and DCT to save thousands of photos on a small memory card.
Photo printing shops: When you give a pen drive with JPEGs, the shop's computer reads DCT-compressed files.
TV and video streaming: Similar transforms are used in video compression (like in MP4), saving internet data.
Why it matters
Saves storage: DCT helps store more photos and videos in less space.
Faster sharing: Smaller files mean faster uploads and downloads, even on slow internet.
Keeps quality: DCT throws away details that our eyes barely notice, so images still look good.
Foundation for other tech: Many other formats (like MP3 for music, MP4 for video) use similar ideas.
Worked example
Let's see how DCT works on a tiny 2×2 block (real JPEG uses 8×8, but this is easier to follow):
Suppose the pixel values are:
52
55
61
66
Step 1: Shift values down by 128 (so, 52-128 = -76, etc):