views:

727

answers:

9

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?

+6  A: 

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.

Andrew Grant
You can "manually" do a 90-180-270 lossless transformation with jpeg.
xpda
+2  A: 

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

Assaf Lavie
360 is doable, but 720 takes more skill
Javier
Hardly! Just do 360 twice.
Assaf Lavie
@Assaf, what about 1080?
Aardvark
1080i, or 1080p?
patros
I've got you all beat: I can rotate a JPEG 0 degrees using just my mind.
MusiGenesis
+2  A: 

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

shoosh
A: 

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.

Ketan
A: 

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.

kigurai
+5  A: 

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."

ISW
+1  A: 

Yes, it is possible for certain cases: 90-degree rotations and flips on images with dimensions that are a multiple of 8. 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.
Boojum
+4  A: 

There is a program named jpegtran

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

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

kcwu
+1  A: 

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.

mloskot