This Demonstration shows how a two-dimensional discrete cosine transform works on the image blocks during JPEG compression and how the data is degraded by the quantization matrix. Red points in the graphs on the right side display zeros, which are important for the file size reduction. More zeros give more size reduction but worse image quality. You can choose photos and images with patterns; most of them are difficult to compress by the JPEG algorithm.

The JPEG compression algorithm (which is also used in MPEG compression) is based on the two-dimensional discrete cosine transform (DCT) applied to image blocks that are 8×8 pixels in size. DCT concentrates information about the pixels in the top-left corner of the 8×8 matrix so that the importance of information in the direction of the bottom-right corner decreases. It is then possible to degrade the low information value coefficients by dividing and multiplying them by the so-called quantization matrix. These operations are rounded to whole numbers, so the smallest values become zero and can be deleted to reduce the size of the JPEG.

In the columns from the left you can see: the original image, the chosen block and DCT coefficients matrix, the compressed image, the compressed chosen block and degraded block and its coefficient matrix, a 3D representation of pixels, the DCT coefficients and degraded coefficients and chosen block output—pixels, zigzag coefficients, and zigzag degraded coefficients.

Half of the sample photo is without any compression and the other half is already compressed by the JPEG algorithm. Can you recognize which half is which by eye? Can you recognize it by the coefficients?