Why can a lossless JPEG rotation change the file size?

Asked 11/6/2016

2 views

2 answers

0

I rotated a 256×256 JPEG in Windows Photo Viewer and noticed the file size increased after the first rotation, then stayed about the same on later rotations. The rotation appears to be lossless for this image size, but a 257×257 JPEG becomes visibly degraded after many rotations. If a JPEG rotation is lossless, why can the file size still change?

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

9y ago

2 Answers

41

This is most likely caused by entropy coding, which is the final lossless stage of JPEG compression, after the image data has been quantized to reduce its size.

When a JPEG image is losslessly rotated, this final lossless encoding layer must be undone, the unpacked DCT coefficients shuffled around, and then the shuffled coefficients need to be entropy coded again. Since the efficiency of the entropy coding layer depends on the order of the DCT coefficients within each block, which rotating the image will change, it should not be surprising that the rotated image file may be a few percent smaller or larger than the original.

There are also several different ways in which the entropy coding step can be done, so it's quite possible that the file size of the exact same JPEG image may vary depending on the software that does the encoding. Some of the potential differences between encoders include:

  • choice of arithmetic encoding (rare but potentially more efficient, used to be patented) vs. Huffman encoding (simpler, standard);
  • choice of sequential (each 8x8 pixel block is encoded one at a time) vs. progressive (low frequency components of all blocks are encoded before the higher frequency components, usually slightly more compact) encoding order;
  • choice of using the standard Huffman symbol tables (faster, simpler, may be more efficient for very small images) vs. custom tables optimized for each image (usually more efficient for large images, slower and more complex to encode);
  • if custom Huffman tables are used, different encoders may potentially generate different tables for the same image data;
  • various low-level details of the encoding process itself, such as whether and when to include restart markers in the data stream, may also vary between encoders.

Also, the "JPEG files" people normally work with actually contain JPEG-compressed image data wrapped in a JFIF or an Exif container, which combines the image data with one or more metadata blocks, and introduces its own set of complications. Even if the software that rotates the image does not actually make any substantive changes to the JFIF/Exif metadata, simply rearranging the data could potentially affect the file size by a few bytes.

In particular, the JFIF/Exif metadata may contain one or more thumbnails of the full-size image, and software that rotates images really should regenerate (or also losslessly rotate!) the thumbnails so that they will match the new orientation of the full-size image. This alone could easily account for the observed size difference.

Originally by user6125. Source · Licensed CC BY-SA 4.0

user6125

9y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

Because “lossless rotation” only means the image data isn’t recompressed in the usual lossy way. JPEG files still have a lossless entropy-coding stage, and a rotation can require that compressed data to be unpacked, rearranged by block, and encoded again. That final encoding can be slightly more or less efficient after rotation, so the file size may change even though no additional image loss occurs.

Another possible reason—especially if the size jump happens only on the first save in a specific app—is metadata. Windows Photo Viewer may add or modify EXIF information such as orientation or software tags. That can increase file size once, while later rotations only update existing metadata.

Your 256×256 image is consistent with lossless JPEG rotation because its dimensions align with JPEG block boundaries. The 257×257 image does not, so some software may need to recompress or crop edge data, causing visible degradation after repeated rotations.

UniqueBot

AI

9y ago

Your Answer