When I make pictures with my camera (Olympus E-520), in the EXIF data the orientation is stored.

The standard image viewer on Ubuntu is displaying this images correctly. Windows viewer however not.

Is there a way to really rotate this images (if necessary according to EXIF) in a batch on Ubuntu? For example with an ImageMagick tool?

up vote 34 down vote accepted

exiftran and JHead (jhead -autorot) can do this. exiftran can do this losslessly, not sure about jhead.

  • 1
    exiftran looks like it's exactly what I need! – Peter Smit Sep 10 '09 at 12:45
  • 2
    jhead -autorot invokes jpegtran, which can do perfectly lossless rotation. I'd forgotten all about the lossless stuff by IJG. jpegclub.org/losslessapps.html Exiftran can also do lossless rotation, and is better than jpegtran because it respects exif information in the image file. You should note however that sometimes (and I'm not sure on all the details) you can't losslessly rotate. So if you implement a script with jhead to identify the necessary rotation (similar to my previous, but with a case statement to handle all rotations) you should support falling back to lossy rotation. – opello Sep 14 '09 at 4:00
  • 1
    I did a few tests with exiftran -a (auto rotate based on exif orientation tag), and it seems to work very well. With regard to lossless rotation, I couldn't get the source image back by performing the inverse of the performed rotation. E.g. exiftool -F 1.jpg -o 2.jpg; exiftool -F 2.jpg -o 3.jpg; md5sum of 1.jpg and 3.jpg are not the same. Upon closer inspection (with Araxis merge binary comparison) sometimes it was just exif header information that differed, other times it was major changes in the file. However Araxis merge's image comparison showed no unchanged pixels. – opello Sep 14 '09 at 5:12
  • I meant no changed* pixels (they were pixel-identical images). Also -- I'm not sure on the stackoverflow/superuser protocol, but I think dmityugov should get this one for recommending the proper tool. – opello Sep 14 '09 at 5:21
  • 1
    I found that exiftran can remove some metadata from file. For example, images taken by Samsung Galaxy S2 have timestamp field (possibly non-EXIF), and exiftran will remove it. exiftran -a also will alter image which doesn't need rotation. jhead -autorot is free from both issues. OTOH, it lacks option to preserve file mtime (but there's -ft switch as a workaround). – pfalcon Aug 21 '13 at 20:00

ImageMagick's convert tool has an -auto-orient flag which should get the job done.



for f in *.jpg
        orientation=$($JHEAD -v $f | $SED -nr 's:.*Orientation = ([0-9]+).*:\1:p')

        if [ -z $orientation ]

        if [ $orientation -gt 1 ]
                echo Rotating $f...
                mv $f $f.bak
                $CONVERT -auto-orient $f.bak $f

I threw together a quick script to iterate over *.jpg in the current directory. You can easily modify this to take in a path ($1) or whatever you need.

  • 1
    Great! That one I didn't find. I use it with mogrify to do a batch of images. One question only, why does it rewrite all images, even if they are not supposed to change? (The hashes differ) – Peter Smit Sep 7 '09 at 4:53
  • I'm not sure why but it seems to do a full re-encode looking at both the image diff and a binary diff. – opello Sep 9 '09 at 0:19
  • Wow, this is great. Thanks very much. If no other supergood answers are added I will surely accept this one. – Peter Smit Sep 9 '09 at 9:20

You can use XnView to do that. Check out these pages for info on using XnView to do auto-rotation in batch mode:

In Windows, you can do that using IrfanView. From IrfanView website FAQ section:

Q: How to use JPG lossless operations (Rotation, IPTC, Comment) in batch mode?

A: Start the Thumbnail window, open the folder with JPGs, select many JPGs and see in thumbnail menu File for JPG Lossless Operations -> Lossless transformations with selected thumbs. Note: The auto-rotation option works only if the EXIF orientation tag is properly saved (not top-left).

With ImageMagick you can also use mogrify to rotate the files and write the rotated image back to the original filenames.

mogrify -auto-orient *.jpg

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.