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.