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?

Giacomo1968
  • 58,727
Peter Smit
  • 9,636

5 Answers5

47

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

Carson Ip
  • 103
user7963
  • 1,517
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.

opello
  • 768
5

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
  • 200
  • 1
  • 11
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
  • 559
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).