38

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?

0

5 Answers 5

47
+100

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

8
  • 1
    exiftran looks like it's exactly what I need!
    – Peter Smit
    Sep 10, 2009 at 12:45
  • 3
    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, 2009 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, 2009 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, 2009 at 5:21
  • 2
    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, 2013 at 20:00
14

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.

3
  • 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, 2009 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, 2009 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, 2009 at 9:20
3

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

0
0

@user7963's answer to use exiftran is right, but how do we use it? Here's what I found:

Tested on Ubuntu 20.04:

Install it:

sudo apt update
sudo apt install exiftran

Use it:

Convert all *.jpg images in dir_of_images to be automatically rotated in place according to their exif orientation metadata:

cd path/to/dir_of_images
exiftran -ai *.jpg

That's it! All *.jpg images will now have been rotated in place!

See man exiftran or exiftran -h for help and details.

Here are what the two options I used (-a and -i) mean:

TRANSFORM OPTIONS
       -a     Automatic (using exif orientation tag).
OTHER OPTIONS
       -i     Enable in-place editing of the images.  Exiftran allows 
              multiple input files then. You must specify either this 
              option or a output file with -o for all operations which 
              modify the  image (i.e.  everything but -d right now).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .