Why JPEG Compression Can Leave a Grid in a Photo

The little squares visible in a heavily compressed photo can trace back to the 8-by-8 blocks that an ordinary JPEG encoder processes separately, as Florida State University’s optical microscopy tutorial shows. Before compression, a gradual change in tone can cross those boundaries smoothly. After the numbers describing each block have been rounded, neighboring blocks can reconstruct with a step in brightness at their shared edge. The step makes a seam visible where there was only a change of shade.

A curved shadow on a tiled floor gives the grid a familiar shape. The curve crosses the square tiles and would still be there if the grout lines disappeared. In a photograph, too, a line or a shadow can pass through several blocks. The squares are the encoder’s units of work. Their borders run straight through whatever happens to be in the picture.

A curved shadow crosses a pale floor laid with square tiles.
A band of shadow curves across the straight joints of a tiled floor.

How does JPEG describe a square of the picture?

For the color images in the Florida State tutorial, the encoder separates brightness information, or luminance, from color information, or chrominance. Changes in brightness do much of the work of making a boundary visible, so the encoder can often discard more color information than brightness information. The same edge can therefore have separate descriptions for its change in tone and its change in color.

Each square is still a patch of samples at this point. The next operation, the discrete cosine transform, describes the same patch as a mixture of patterns. Some patterns change gently across the square. Others alternate more rapidly from light to dark. A coefficient is the number that tells the decoder how much of a particular pattern to use. A patch with little detail usually needs little contribution from the rapidly changing patterns.

In his technical account of the JPEG standard, Gregory K. Wallace calls the weight for the average level across the patch the DC coefficient. The other weights describe departures from that level. On a brightness patch, a change to the average shifts the tone of the entire square, while a change to a finer pattern alters the light and dark variation within it. Each square has an average of its own, even where a shadow flows continuously from square to square.

A pixel-by-pixel description gives the values at particular positions. The transformed description gives the amounts of the patterns that add up to those values. Wallace explains that with ideal arithmetic and the coefficients retained at full precision, the inverse transform could recover the original samples exactly. The transform prepares the description for the step that makes the file lossy.

From a Patch to a Set of Patterns
  1. Image samples:Separate brightness and color information
  2. Square patches:Transform each block on its own
  3. Pattern weights:Describe how much each broad or finer pattern contributes

Where does the rounding take detail away?

Quantization is the step that reduces the precision of the coefficients. As the Florida State tutorial explains, the encoder divides each coefficient by the corresponding value in a table and rounds the result. The table lets it use different increments for different patterns. A larger divisor makes for coarser steps: more original values end up represented by the same rounded number.

Small coefficients can round all the way to zero. That commonly happens to coefficients for fine patterns, so light and dark variations that depended on those patterns can lose definition. More aggressive quantization can also change the coefficients that describe broader changes in tone. The losses depend on what is in each patch of the original photograph. A patch full of specimen detail can lose variations that an almost even patch never contained.

What Happens to a Small Coefficient
  1. Divide: Express the coefficient in the increment set by the table
  2. Round: Store the nearest integer, which can be zero for a small contribution
  3. Reconstruct: Use the stored value to set the strength of that pattern

Rounding also makes the file easier to pack. The tutorial describes how the encoder reads the rounded values in a zigzag, from the broad patterns toward the finer ones. The zeros often collect toward the end. A run of zeros can be stored compactly as a run rather than written out value by value. The familiar coding method also uses Huffman coding, a reversible way of representing the resulting data. The file preserves a zero through that last step as faithfully as it preserves any other rounded value. Quantization already removed the contribution that the zero replaced.

Wallace describes a different shortcut for the block average. The encoder can store how much the rounded average coefficient changed from the previous block in coding order. Those values tend to be close, so the difference is often small. This shortcut records the rounded average exactly; the earlier rounding still determines its precision.

On opening the file, the decoder uses the stored coefficients and the quantization table to build the patches back up. Wallace describes the inverse transform as summing the patterns to reconstruct the samples. Each side of a seam comes from a separately rounded description, so the tones at the join can differ by more than they did in the original. A change that was gradual across the boundary can then look abrupt. When enough joins show through at once, the picture takes on the geometry of the blocks.

The table tells the decoder the size of the step between stored values. Multiplying by that step puts each weight back on the scale the transform used. The decoder recovers the value represented by the rounded number. A value that landed at zero stays there, and even different original weights that landed at the same rounded value come back as the same weight. The resulting samples are drawn in their original positions, which is why the seam can run through the middle of an object rather than tracing its contour.

In their study of heavily compressed images, image-processing researchers Deqing Sun and Wai-Kuen Cham report that blocking is especially obvious in smooth regions. A gradual background gives an abrupt step little competition. A busy patch has more of its own edges, so a seam can be harder to pick out there even when the patch has lost detail. The areas where the grid stands out and the areas where fine features have suffered can be different parts of the same photograph.

That geometry is a clue when a saved image looks rough. Nikon’s MicroscopyU tutorial on digital sensors describes noise during capture as apparently random fluctuations in signal intensity; shot noise, for example, comes from statistical variation in the arrival of light at the sensor. Shot noise can give a patch a fine, uneven texture. Blocking shows up along regularly spaced rows and columns. A photo can contain both, and the straight joins are the clue specific to blocking.

What happens to the microscope specimen?

Florida State’s tutorial places a specimen image beside a compressed version of that same image. Its description of the display gives each pane a separate job. The left shows the reference; the right shows what the encoder and decoder have made from it as the compression setting changes. The pane on the right can also be switched to a difference image. That view draws on both versions to show where their pixel values diverge.

A glass slide rests on the stage beneath a microscope objective.
A glass slide sits beneath the silver objective of an optical microscope.

At a less aggressive setting, the compressed pane can still show a recognizable specimen. The shape and the local details are different tests of the result. A broad outline can survive while features inside it lose definition. At the strongest settings, square boundaries start to compete with the shapes in the specimen. Stephen P. Price and Michael W. Davidson of Florida State’s National High Magnetic Field Laboratory and their fellow tutorial contributors describe the result: "At the highest compression ratios, 8 x 8 blocking artifacts occur, which mask many of the image features."

Three Views of the Microscope Specimen

Scroll sideways to see all columns.

Data behind the viewWhat the tutorial displays
Original pixel values, retained as the referenceThe specimen before JPEG compression
Rounded pattern weights for each squareThe reconstructed specimen, with visible seams where neighboring blocks no longer meet smoothly
Both sets of pixel values togetherA difference view that locates changes across the specimen

The tutorial says that the same movement of its slider can lead to different compression ratios depending on the specimen. The details of the starting image affect how compactly the encoder can pack its description. The ratio compares the uncompressed and compressed sizes. The tutorial also displays a quality setting and an error measure for the image as a whole. Only the difference view shows where the errors occur. A change near a specimen’s edge can matter more to someone measuring its outline than one in a blank corner.

On the tiled floor, the grout belongs to the floor and the shadow crosses it. In the microscope comparison, the reference shows which edges belonged to the specimen before the squares appeared. In the compressed pane, part of what looks like the specimen’s texture is the seam between patches of reconstructed brightness.

Frequently Asked Questions

What are compression artifacts in photography?

Compression artifacts are visible changes introduced when an image is encoded and reconstructed. Sun and Cham describe another such effect around strong edges: ringing, a ripple in tone close to an outline. While blocking follows the square patches, ringing can follow the edge of the pictured object.

What kind of compression does JPEG use?

The familiar JPEG method uses a transform, lossy quantization, and then reversible coding of the rounded data. The Joint Photographic Experts Group’s overview also lists a separate lossless coding option within the standard. In that mode, exact recovery of the source samples is the aim, a distinction Wallace makes in his account of the coding modes.

Can a grayscale JPEG show compression artifacts?

Yes. The Florida State tutorial can load a grayscale version of the chosen specimen and compress it directly. Quantization still acts on the patterns that describe brightness, so a grayscale image can show the square seams even though the comparison has no color data to follow.

Can a compressed microscopy image reveal exactly which details were lost?

A compressed microscopy image can show signs of loss, such as conspicuous seams, but locating an exact change requires the reference image. The tutorial’s contributors caution that losses vary with local detail. Their example distinguishes an image used to show the general appearance of a specimen from one used to study the dimensions or intensities of its features.

Sources

  • Florida State University: JPEG Image Compression
  • Joint Photographic Experts Group: Overview of JPEG 1
  • IEEE Transactions on Consumer Electronics: The JPEG Still Picture Compression Standard
  • Nikon MicroscopyU: CCD Signal-to-Noise Ratio
  • IEEE Transactions on Image Processing: Postprocessing of Low Bit-Rate Block DCT Coded Images Based on a Fields of Experts Prior