PetaPixel

Why You Should Always Rotate Original JPEG Photos Losslessly

Recognize the warning message above? It’s what Windows XP would show whenever you tried to rotate a JPEG image 90° using the Windows Picture and Fax Viewer. If you’re like me, you probably didn’t think twice about it (and checked the checkbox), since you had done it many times already and hadn’t noticed any difference in quality. After all, how hard can it be to turn a digital photo sideways? You just move the pixels around right?

Well, not really. The fact of the matter is, JPEG is a “lossy” compression algorithm that’s geared towards storing and sharing photos without taking up too much disk space. Rotating these compressed images is usually done by decompressing, rotating, and then re-compressing. Since the re-compression is lossy (i.e. data is thrown away), this process results in slightly degraded photos (hence that warning).

If you’re not convinced, check out this interesting experiment done by a Wikipedia user named Burny. Using a simple computer script, he took the following image and rotated it lossily 2000 times:

Here’s hows the image degraded through those 2000 rotations:

Pretty crazy, huh?

The term for this is “generation loss“: the loss of quality between subsequent “generations” of data.

Through multiple generations, photocopied papers, copied VHS tapes (remember those things?), and lossily compressed JPEGs all become muddled.

We’ve actually written about this subject a few times before. Here’s what happens if you save a JPEG (lossily) hundreds of times and what happens if you upload and download a YouTube video thousands of times.

Here’s what happens if you copy a VHS tape just 23 times:

In the same way, every time you rotate a JPEG lossily, you’re throwing away another fixed percentage of the data. It’s not much at first, but over multiple rotations this adds up.

As the Windows alert box suggests, you should always work on copies of your original files anyway. If you must rotate an original JPEG, try using a program that does it losslessly.

Don’t think you’re safe just because you use Windows 7 and the built in Windows Photo Viewer. While it generally does lossless rotations, in some cases it still functions lossily.

Matt Grum explains over on photo.stackexchange:

If the dimensions of the image are multiples of 8 (or 16 if chroma subsampling is used) then the rotations are lossless. Otherwise it is not possible to rotate the image without recomputing the blocks i.e. recompressing the image, which is lossy.

The reason for this is that jpeg images are broken up into a series of 8×8 or 16×16 blocks which are compressed individually. Incomplete blocks are only allowed on the right edge and bottom edge. Thus is your image is not an exact multiple of 8/16 it will contain incomplete blocks, which will end up on the wrong edge after rotation.

The good news is, most modern digital cameras produce photos that allow for this lossless rotation, so this isn’t an issue that will affect most of your image handling (it’s still good to be aware of, though).

Finally here’s another solution to this lossless/lossy problem: shoot RAW. You can spawn as many JPEG photos as you want from your RAW files, and then rotate them to your heart’s content!


 
 
  • 9inchnail

    Why do you have to decompress the photo before rotating in the first place? Why not just move the pixels the way they are?

  • 11

    Devil is in the detail. When you re-compress after a rotate step, you should use the same compression factor. Yes, there can still be losses in this step because of numeral error even if you choose the same compression factor. If you do it 1000s of times, the numeral errors will blow up and dominate. I think iphone just stores the fact that you rotated the image rather than actually rotating the imge.

  • Guest

    There are no “pixels” in a compressed jpeg. JPEG isn’t a pixel map, it’s basically a description of how “blocks” of pixels interact. A blue sky in a jpeg isn’t a series of blue pixels, it’s a description that a block this big is a field of blue with this sort of properties. It might be possible to write something to actually rotate that, but it would be complicated and it would only work on compressed jpegs. General purpose image manipulation utilities operate on pixel maps, so if you want to use one to rotate a jpeg, you have to make it a pixel map first.

  • Puue

    Huh. I had no idea.

  • dll

    Burned discs shouldn’t degrade as the information is digita, or am I missing something? Maybe if the error correction algorithm fails and the error is copied onto the new disc…

  • Simeon Pilgrim

    The devil is in understanding how the data is “estimated” by the lossy compression. If you didn’t alter the image in any way, and used the exact program with the same settings, you might get no error lose.

    But given the image is rotated, the relationship between pixels is changed. Also the next compression try’s “keep the detail” that was the previous round’s noise, thus more bits go towards representing the relative difference which is the noise. The allocation of more space to the noice leaves less bits for detail, thus white the picture degrades to white noise.

  • does not apply in this case. Disc copy is a loss-less data transfer.

  • http://www.facebook.com/bart.willems Bart Willems

    Well, if you rotate a raw, save it as a jpg, and then rotate that that file, save it, repeat 1000 times, you’ll get nearly the same result as starting with a jpeg. So really you don’t gain anything from starting with a raw file.
    Unless you mean for every of those 1000 rotations use the raw file as the basis. But why not do the same when (heaven knows why) you want to create 1000 rotated versions of you jpeg, juse the original jpeg as the basis, not the copy of the copy of the copy?
    What it comes down to is “don’t keep modifying and saving the same jpeg file over and over”.

  • rigelt

    this article is so very flat… doh. How can you write about all this without mentioning the degree of compression in JPEG settings dialog (quality) and the possible reason, why Microsoft didn’t implement it in some of their consumer products. An article about lossy vs. lossless image formats and how to include which formats in the own image processing workflow would make more sense.
    @ dll: burned data discs shoudn’t lose data that’s correct. But it’s a whole different story when it comes to audio CDs. For various reasons it can become quite challenging to complete an exact extract of an audio CD.

  • http://www.petapixel.com Michael Zhang

    You’re right. We’ve taken that bit out. Thanks.

  • http://www.petapixel.com Michael Zhang

    What I meant was, using a RAW file as a starting point ensures that your lossy operations are done on copies, not the original file. The JPEG’s degradation will definitely be the same :)

  • Graysmith

    Does Photoshop rotate lossy or lossless?

  • The_photographer_Tom

    Seriously. Who’s going to rotate a JPG 2000 times?
    I may rotate a JPG once, but I definitely don’t feel the need to rotate it again.

  • http://twitter.com/kn00tcn kn00tcn

    photoshop makes you resave doesnt it?

  • romanu

    They are exacerbating the effect in order to show the lossy method used in rotating pictures with dimensions that are not multiples of 8.

  • romanu

    it’s not a manner of what you use to rotate it, rather it’s the input you use. In their simulation, they write a new file every time they rotate; in photoshot, your rotations only impact once you save the image.

  • Matt

    It is not the rotation that is the issue. It is resaving it that does the damage. Each generation will degrade regardless if you altered anything. Make sure you do not ‘save’ a jpg if you did not alter anything, just close it.

  • Griffyn

    Burned discs do degrade, but simply over time. It’s actually the opposite case, as copying a burned disc will renew it and give it another 3-5 years.

  • vorno

    There are a small handful of apps that specifically can rotate and save a JPG without losing information and/or degrading image quality.

    I believe this works by not recompressing the image, but merely translates the blocks of information that make up the image, using a low-level understanding of the JPG format.

  • William Bennett

    Not trying to spark a debate over PC or Mac but anyone know if there is a similar issue with using the Preview app that is standard on Macs?

    I know that Preview has a feature [at least in OS X 10.7 Lion] that has a quality slider for JPEG and JPEG-2000, I would “assume” that the degradation would be true if a lower quality setting was selected. When I work with JPEG’s and rotate them in Preview, there is no indication of image quality loss due to the rotation.

    This is taking into account you were working with jpeg’s not residing on a CD/DVD or other “read-only” data storage.
    Obviously, if you changed anything or adjusted the size or output that is understandable, but since there is no prompt of the reduction in quality I wonder if there is any, on a Mac that is.

    Anyone know for sure?

    I posted 2 pics when I rotated a jpeg, but the file size seemed to grow a little.

  • the Trouble

    I think this is a Windows only problem -especially Windows XP related software… Win7 is a bit better I think.

    I’m am using Linux and it does not save “anything” automatically without the user permission.
    I do not know about the OsX, but because it is *x-based, surely it will work like any Linux-system -without problems. Only worry is that Mac may do something behind what it does not tell you its doing so…you’ll never know.

  • Mark_Ransom

    Lossless rotation will not lose or change any of your image data. However it might lose some of the EXIF information, especially the part that’s specific to your camera brand. Keep your original intact and make a copy to rotate, no matter how you choose to rotate it.

    When resaving a JPEG you usually have an option for setting the quality. There’s a belief that using the same quality setting that was used originally will not result in any loss, but I believe that’s a myth. If you have an image editor that can do image subtraction or create a layer with a difference mode, it’s easy to prove to yourself – save an image as JPEG, close it, then repeat 4 times: open the latest JPEG, rotate it 90 degrees, and save to a new file. All 5 files should have been saved at the same quality setting, and the first and last should be the same rotation. Compare the two files and see what you get.

    If you save a JPEG at the highest quality setting you’ll minimize the loss, but you won’t prevent it – and the file size will be larger than the original.

  • Mark_Ransom

    Lossless rotation is something that you must do BEFORE you open the file. I don’t know enough about Photoshop specifically to know if it can.

  • http://twitter.com/FrankBuckleyWor Frank Buckley

    It is a good idea to use Picasa (free).
    This always keeps the original picture untouched and writes a text file listing the required changes which it applies when you view the picture.
    You can save a copy of your modified picture as another jpg or png, but these are fairly sure to have some degradation, but only one generation of change.