Can a TIFF be rotated 90° without re-encoding or increasing file size?
Asked 5/27/2016
3 views
2 answers
0
I’m looking for a way to rotate a scanned TIFF by 90° without recompressing or otherwise altering the image data. In Preview on macOS, rotating and saving increases the file size, and it may also add an alpha channel. Other editors I tried also made the TIFF larger even when using the same compression settings. Is there any application that can do a true lossless 90° TIFF rotation, or is changing file size unavoidable?
Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0
Photography Stack Exchange contributor
10y ago
2 Answers
5
It is possible and even trivial but I am not aware of any application to do that task specifically.
There reason why the file size changes when rotated is that TIFF files are encoded losslessly as one would compress a stream of pixels components from one corner of the image to the opposite one. If you consider Run-Length-Encoding (RLE) which a common TIFF encoding, it is easy to understand why the same image will not compress to the same size after being rotated. It is equally likely that the file increases in size than it would decrease.
There is an easy way to get around that though and that is simply to change the tag which says which way pixels are ordered in the image. By simply changing the tag from a row-oriented ordering to the equivalent column-oriented one, the image is effectively rotated without any change to pixel data. You can read the details about the TIFF format here, just search for tag 274 (Orientation).
Honestly I thought no one used column oriented storage pixels so when I initially wrote a TIFF codec, I ignored that possibility for simplicity. As it would happen, one day we received such a TIFF image and my code did crash! Hopefully not many other developers took that short-cut either. It turns out that the file in question came from a scanner which did the scanning and encoding always exactly in the same way but set the orientation flag depending if it was fed a film-strip with frames in landscape or portrait orientation.
Originally by user1620. Source · Licensed CC BY-SA 4.0
user1620
10y ago
0
Generated from our catalog & community — verify before relying on it.
Usually, a 90° TIFF rotation requires the image data to be rewritten, so the file may be recompressed and end up a different size even though the operation is lossless. With TIFF compression methods such as RLE or LZW, rotating changes the pixel order, so the compressed data often won’t pack to the same size.
In theory, a TIFF can be “rotated” without re-encoding by changing the TIFF orientation/tag metadata so software displays it differently. That avoids rewriting pixel data, but it depends on software honoring that tag and is not the same as physically rotating the stored pixels.
From the programs tested in the community answers, none performed a true no-reencode pixel rotation. Among them, ToyViewer produced the smallest file increase, with Xee and IrfanView also doing better than Preview. Preview additionally adds an alpha channel, which increases file size further.
So: true pixel rotation without rewriting is generally not what typical apps do; metadata-based orientation is the closest workaround.
Recommended products
UniqueBot
AI10y ago