Can a JPEG compressed image be rotated without a loss in quality?

JPEG is a lossy compression scheme, so decompression-manipulation-recompression normally reduces the image quality further for each step. Is it possible to rotate a JPEG image without incurring further loss? From what little I know of the JPEG algorithm, it naively seems possible to avoid further loss with a bit of effort. Which common image manipulation programs (e.g. GIMP, Paint Shop Pro, Windows Photo Gallery) and graphic libraries cause quality loss when performing a rotation and which don't?

Answers


Yes, it is possible for certain cases: 90-degree rotations and flips on images. The heart of the JPEG algorithm -- the lossy part -- involves breaking the image into 8x8 pixel blocks, performing a discrete cosine transform on the block and then quantizing the result. There's also some color space conversion and lossless compression of the blocks on top of this.

Rotating or flipping an 8x8 block will give a DCT with the same basic coefficients, but possibly transposed and/or with some sign changes depending on the transformation. So the basic steps to rotate or flip an image losslessly would involve:

  1. Decompress and extract the blocks
  2. Transpose and/or sign flip the DCT coefficients for each block
  3. Reshuffle the blocks into their new order (otherwise the 8x8 blocks would be rotated but still in the old place)
  4. Recompress it all with the lossless compression steps.

There is a program named jpegtran

jpegtran – a utility for lossless transcoding between different JPEG formats.

To rotate the image losslessly, you can do the following:

$ jpegtran -rotate 180 -perfect -outfile rotated.jpg origin.jpg

And Here is a list of applications which provide the JPEG lossless rotation feature based on the IJG code


Absolutely - just change the orientation value in the EXIF data. The vast majority of image programs will respect this setting and show the picture "rotated".

It's also possibly to "manually" (e.g. programatically) rotate the image in a lossless fashion if certain criteria are true - rotation must be 90/180 degrees and the width/height must multiples of the block-size. You can also flip/mirror it. I don't know whether image programs are smart enough to special-case this operation though. I would guess not.


From the JPEG FAQ:

"There are a few specialized operations that can be done on a JPEG file without decompressing it, and thus without incurring the generational loss that you'd normally get from loading and re-saving the image in a regular image editor. In particular it is possible to do 90-degree rotations and flips losslessly, if the image dimensions are a multiple of the file's block size (typically 16x16, 16x8, or 8x8 pixels for color JPEGs). ...

But you do need special software; rotating the image in a regular image editor won't be lossless."


Not a jpg expert, but it seems that the answer would be Yes for 90, 180, 270 degree rotations. (maybe even for 360! :))


Yes, it is possible. A quick google search gave this list of programs which do this


According to the excellent article on Understanding Digital Image Interpolation by Sean McHugh:

Interpolation also occurs each time you rotate or distort an image. (...) The 90° rotation is lossless because no pixel ever has to be repositioned onto the border between two pixels (and therefore divided).

and eventually concludes with

avoid rotating your photos when possible; if an unleveled photo requires it, rotate no more than once.


If you are talking of rotating a JPEG image then there is no further compression right? It is about rotating pixel locations.

Doing rotation with any program will potentially change intermediate dimensions, as it needs to preserver original image, this may be an issue to consider.


Unless you rotate by multiples of 90 degrees then your image will have to perform some kind of interpolation which might reduce the quality of your image. Using a good interpolation algorithm will help here.

As for opening and recompressing, I am not sure you would actually get worse quality, but then I am not sure exactly how JPEG works.

I suggest you try to compress, manipulate and recompress and see for yourself if the result is good enough. What is good enough is subject to your application.


If it can help :

Trying to do better than the Microsoft (Windows 7) native picture viewer and its right-click rotation options, I tried several apps of the following link : http://jpegclub.org/losslessapps.html

For instance, I tried FastStone Image Viewer, XnView, Photosurfer, JPEG Lossless Rotator, ExifPro Image Viewer.

NONE of them yields a bigger picture than the basic Windows 7 picture viewer after a simple 90° rotation. It's admittedly limited to conclude so quickly, but I still don't have found a real lossless rotation .jpeg app for the moment, and in any case not better than the built-in Windows one.


Need Your Help

Git clone with custom SSH using GIT_SSH error

git ssh environment-variables clone git-clone

I am trying to clone a Git repo using a custom SSH command. I set the SSH command in the GIT_SSH environmental variably be running