oss

Why one missing row can stop a JPEG from turning sideways

4 sources 3 primary sources November 25, 2026

Loading reads and saves…
Text

Take the square portrait of astronaut Eileen Collins that ships with the open-source image library scikit-image. Save it as a JPEG, and the open-source utility jpegtran can turn it a quarter turn clockwise without another generation of compression loss. Now make a second JPEG from the same portrait, leaving off the bottom row. Ask for the same perfect turn and the program refuses. The missing row was nowhere near Collins’s face. What could it possibly matter to a rotation? [1][2]

Eileen Collins in an orange flight suit, with a dark helmet in the foreground and a shuttle model behind her.
NASA’s portrait of Eileen Collins, distributed by scikit-image as a 512-by-512-pixel sample. Both JPEGs in the rotation test start with this photograph. [1]

What goes around with the helmet

Inside a conventional JPEG, an eight-by-eight block of brightness samples is represented by a table of coefficients. One describes the average level across the block. Others describe patterns of change at different frequencies along the horizontal and vertical axes. When the photograph first becomes a JPEG, the encoder divides those coefficients by entries in a quantization table and rounds the results. Some fine distinctions along the rim of Collins’s helmet disappear into the rounding. [2][3]

Opening the JPEG as pixels and saving the rotated pixels as a new JPEG puts the rim through that rounding step again. The libjpeg-turbo implementation of jpegtran takes a shorter path. It reads the stored coefficients, moves whole blocks to their new positions, and turns the pattern inside each block. A horizontal variation becomes a vertical one. In the routine for a clockwise quarter turn, the software exchanges the two frequency axes and changes signs on alternating columns of the destination coefficient table. It can do that work on the integers it already has. The detail that survived the first save survives the turn. [2][3]

The last fifteen rows

For the test, both JPEGs used 4:2:0 color sampling. In this arrangement four brightness blocks share a pair of color-difference blocks, so the encoder handles a patch spanning sixteen pixels in each direction. The square portrait contains exactly 32 such patches down each side. Its shorter twin measures 512 by 511. After 31 complete rows of patches, fifteen visible pixel rows remain at the bottom. JPEG can encode the last patch by padding it, then use the image dimensions to tell a decoder where the photograph ends. [2][3]

Now picture turning a print clockwise on a table. Its bottom edge becomes its left edge. On the shortened JPEG, the padding would arrive on the left, ahead of the visible photograph. The ordinary JPEG grid starts at the upper left. A width of 511 tells the decoder where to stop on the right. It has no corresponding instruction to skip a padded column on the left. Getting every visible sample into the desired position would shift the photograph across its block boundaries. Moving intact blocks can no longer do the whole job. [2][3]

What the command promises

With the full-size JPEG, jpegtran -perfect -rotate 90 -outfile turned.jpg full.jpg succeeds. With the 511-row copy as input, it reports that the transformation is imperfect and exits with an error. The switch makes success mean something precise enough for a batch job to rely on. A pipeline that needs every row can set the file aside at that point. [2]

There are two other possible outcomes. Leave off -perfect and the tool preserves the partial edge with special handling, which can leave a misplaced strip. Choose -trim and the output from the short portrait measures 496 pixels wide by 512 high. Losing a single row before encoding has made fifteen more rows ineligible for the trimmed turn. NIST’s fingerprint image software manual illustrates this same command and explicitly describes the edge pixels it discards. For a collection of photographs, “trim” is a decision about which part of the subject to keep. [2][4]

The shorter portrait still admits one revealing edit. Run jpegtran -perfect -transpose on it and the operation succeeds. Transposition exchanges row and column coordinates across the diagonal. The partial bottom edge becomes a partial right edge, exactly where a JPEG decoder already knows how to stop. The same file, the same fifteen rows, and a different destination for the padding are enough to change the answer. [2][3]

Sources

  1. scikit-image, sample data documentation for the NASA astronaut photograph.
  2. libjpeg-turbo, usage guide for JPEG encoding, sampling and jpegtran transformations.
  3. libjpeg-turbo, transform implementation in transupp.c, including block movement, coefficient transposition and edge handling.
  4. Michael D. Garris and colleagues, User’s Guide to NIST Fingerprint Image Software, jpegtran reference and trimming example.
Previous The instrument that went missing from “Tekkniko” Next How far should a paddle move on an Arduboy?

Recommended In oss

Matched by subject and format