30

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?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Mat
  • 82,161
  • 34
  • 89
  • 109

10 Answers10

22

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

Mike Burton
  • 3,010
  • 24
  • 33
kcwu
  • 6,778
  • 4
  • 27
  • 32
  • 1
    Note: the corresponding package is named `libjpeg-turbo` on ArchLinux. – VasiliNovikov Feb 18 '17 at 21:56
  • 3
    WARNING: I also found out now that jpegtran _can in fact_ lose quality. It probably happens to jpeg which size is not diviseable by 16. Comments from knowing people welcome. – VasiliNovikov Feb 27 '17 at 17:20
  • @VasiliNovikov The manual page has details. I haven't used the rotate operation, but I know that for the crop operation it will only be lossless if you choose the crop boundaries along compression block boundaries. Possibly for a rotation that is as lossless as possible you would first need to crop partial blocks alonge the borders and then rotate the result. – Nobody Dec 05 '20 at 12:54
  • @Nobody the `-perfect` key makes either the operation succeed and give a lossless rotation, or it will not produce any output at all and fail. – VasiliNovikov Dec 05 '20 at 16:48
  • thanks man, even the file size does not change :) – danday74 Dec 07 '21 at 14:52
15

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.
Liggliluff
  • 706
  • 1
  • 6
  • 22
Boojum
  • 6,592
  • 1
  • 30
  • 34
  • 2
    Is there a java library that does this? – android developer Feb 05 '14 at 10:32
  • So just to be clear that's (90° rotations + flips) on images with dimensions a multiple of 8, not 90° rotations + (flips on images with dimensions a multiple of 8)? – Mark Fisher Mar 30 '18 at 09:21
  • 1
    @MarkFisher -- Yes, the former. Though come to think of it, the image must be padded (e.g., https://dsp.stackexchange.com/a/35343) if necessary so that it has complete 8x8 blocks for the DCT. So I was mistaken about dimensions being a multiple of 8 being required. – Boojum Apr 03 '18 at 18:29
  • Hello @Boojum, could you please see [this question](https://stackoverflow.com/questions/64286451/jpeg-image-rotation-in-c-using-libjpeg), i think your input will be helpful for in place JPEG image rotation. – Aasim Oct 12 '20 at 05:05
13

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
  • 58,260
  • 22
  • 130
  • 143
  • You can "manually" do a 90-180-270 lossless transformation with jpeg. – xpda Jan 27 '10 at 16:19
  • 3
    changing the Exif of the image won't do anything to the data itself, and some image viewers don't treat it well. plus, it's a bit harder to play with in case you wish to modify the image, since you have to consider the orientation... – android developer Feb 05 '14 at 10:34
  • Qt's `QImage`/`QPixmap` does not take the exif orientation field into account. – Gabriel Jan 19 '20 at 22:43
  • While uploading to online forums or most websites in general strip "EXIF data" out of a photograph as it can leak sensitive data to track a person (eg. coordinates). I think no browser supports reading image according to EXIF data. – tripulse May 07 '20 at 01:23
10

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
  • 11,110
  • 3
  • 25
  • 27
2

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
  • 73,079
  • 34
  • 148
  • 203
2

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

shoosh
  • 76,898
  • 55
  • 205
  • 325
0

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
  • 37,086
  • 11
  • 109
  • 136
0

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.

user3094822
  • 5
  • 1
  • 1
  • 3
0

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.

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
  • 1
    Opening and recompression, especially multiple times, would definitely result in worse quality. This is a fundamental weakness of lossless compression. (Whether or not it is worse enough to care is definitely subjective as you suggest.) – John Melville Feb 21 '11 at 15:49
-1

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
  • 1,017
  • 8
  • 17