How to rotate images automatically, based on exif data

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


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

#!/bin/bash

JHEAD=jhead
SED=sed
CONVERT=convert

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

        if [ -z $orientation ]
        then
                orientation=0
        fi

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

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.


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

  • http://newsgroup.xnview.com/viewtopic.php?t=1420
  • http://newsgroup.xnview.com/viewtopic.php?t=13424 (check out "Follow orientation" option)
  • http://graphicssoft.about.com/od/xnview/qt/batchresize.htm (check out "Follow orientation" option)

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