How to rotate images automatically, based on exif data

31

10

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?

Peter Smit

Posted 2009-09-06T13:54:59.300

Reputation: 7 906

Answers

38

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

user7963

Posted 2009-09-06T13:54:59.300

Reputation: 1 397

Works for me : ls *.JPG | xargs -I {} exiftran -i -a {} – mrgloom – 2017-11-24T16:07:59.270

1exiftran looks like it's exactly what I need! – Peter Smit – 2009-09-10T12:45:52.670

3

jhead -autorot invokes jpegtran, which can do perfectly lossless rotation. I'd forgotten all about the lossless stuff by IJG. http://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 – 2009-09-14T04:00:40.490

1I 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 – 2009-09-14T05:12:08.323

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 – 2009-09-14T05:21:01.560

1I 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 – 2013-08-21T20:00:17.217

12

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.

opello

Posted 2009-09-06T13:54:59.300

Reputation: 696

1Great! 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 – 2009-09-07T04:53:08.713

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 – 2009-09-09T00:19:25.230

Wow, this is great. Thanks very much. If no other supergood answers are added I will surely accept this one. – Peter Smit – 2009-09-09T09:20:32.120

2

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

swatkat

Posted 2009-09-06T13:54:59.300

Reputation: 549

1

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

Andy Gee

Posted 2009-09-06T13:54:59.300

Reputation: 133