L
LLLOS.ai
Learn
L

Ask Your Tutor · Computer Science

How does the Discrete Cosine Transform (DCT) work in JPEG?

General knowledge In depth Asked by Anonymous from Seattle, United States 23 Oct 2026, 10:50 PM IST Updated

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 — 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

  1. Divide the image into 8×8 blocks: The JPEG process cuts the image into small squares, each 8 pixels by 8 pixels.
  2. 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.
  3. Apply the DCT: For each 8×8 block, the DCT formula is used. It turns the 64 pixel values into 64 DCT coefficients (numbers).
  4. 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.
  5. 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.
  6. 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
  1. Divide image into 8×8 blocks
  2. Shift pixel values down by 128
  3. Apply DCT to each block
  4. Quantize (round off) DCT numbers
  5. 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:

5255
6166

Step 1: Shift values down by 128 (so, 52-128 = -76, etc):

-76-73
-67-62

Step 2: Apply DCT formula (simplified for 2×2):

  • DCT[0,0] (average): (-76 + -73 + -67 + -62)/2 = -139
  • DCT[0,1] (horizontal): ((-76 + -73) - (-67 + -62))/2 = (-149 - -129)/2 = -10
  • DCT[1,0] (vertical): ((-76 + -67) - (-73 + -62))/2 = (-143 - -135)/2 = -4
  • DCT[1,1] (diagonal): ((-76 - -73) - (-67 - -62))/2 = (-3 - -5)/2 = 1

Step 3: Quantize (divide by standard values and round, let's say all are divided by 10):

  • DCT[0,0]: -139/10 = -14
  • DCT[0,1]: -10/10 = -1
  • DCT[1,0]: -4/10 = 0
  • DCT[1,1]: 1/10 = 0

Step 4: Store only the non-zero numbers: -14, -1

Common mistakes

  • Thinking DCT is the same as JPEG: DCT is just one step in JPEG compression, not the whole process.
  • Forgetting quantization: DCT alone does not compress; quantization (rounding off) is what reduces file size.
  • Believing DCT throws away important details: It mainly removes details our eyes can't see, so the image still looks good.
  • Assuming all blocks are compressed equally: Some blocks (like with sharp edges) keep more details; smooth blocks lose more.

Remember it

DCT in JPEG is like breaking an image into patterns, keeping only the important ones, and tossing the rest to save space.

Check yourself

Why does JPEG use DCT instead of just saving the pixel values directly?

Because DCT separates important patterns from tiny details, making it easier to compress the image without losing visible quality.

What happens to most of the DCT coefficients after quantization?

Most become zero, so they don't need to be stored, reducing the file size.

If you see a blurry JPEG image, what step in the process likely caused it?

Too much quantization, which removed too many details.

Name one real-life place where DCT is used every day.

On your phone, every time you take or send a JPEG photo.

Quick recap

  • DCT turns image blocks into patterns (frequencies) for easier compression.
  • JPEG splits images into 8×8 blocks and applies DCT to each block.
  • Most DCT numbers become zero after quantization, saving space.
  • DCT keeps important image details and removes what our eyes barely notice.
  • DCT is used in almost every JPEG photo you see or share.
Written by the LLOS.ai tutor. Asked by Anonymous from Seattle, United States on 23 Oct 2026, 10:50 PM IST