News:

2023-08-10 - ExifTool version 12.65 released

Main Menu

Detecting Orientation

Started by Archive, May 12, 2010, 08:54:24 AM

Previous topic - Next topic

Archive

[Originally posted by knighthawk on 2008-08-22 13:10:34-07]

Hi Phil,

I'm trying to detect the orientation of a photograph so that I can use jpegtran to perform a lossless rotation.

However I get conflicting values from different photographs. These are some of the results:

* Orientation = top - left  

* Orientation = Horizontal (normal) -- this seems odd because the image has been rotated taken out in portrait mode.

* Rotate 90 CW (from another thread in this forum)

In addition, I also have this parameter:
Auto Rotate  : Rotate 90 CW

If it helps in anyway, the camera is Canon Powershot A530.

How would I collect the rotation angle (clockwise or anti-clockwise) to:  
Code:
my $rotation?

Archive

[Originally posted by exiftool on 2008-08-24 11:34:25-07]

The Orientation tag should give you what you need.  The tag
is set by the camera to the camera Orientation, but may be set
back to Horizontal if the image is rotated by software.

I found the "top-left" nomenclature a bit confusing, so instead
exiftool describes the transformation that corresponds to the
specified orientation.  You can always use -n to obtain the
numerical Orientation value if you prefer.

I do this in my code:

Code:
my %rot_args = ( 2 => '-flip horizontal',
                 3 => '-rotate 180',
                 4 => '-flip vertical',
                 5 => '-transpose',
                 6 => '-rotate 90',
                 7 => '-transverse',
                 8 => '-rotate 270' );
my $rotation = $exifTool->GetValue('Orientation', 'ValueConv');
my $command = "jpegtran -copy all $rot_args{$rotation} ...";

- Phil

Archive

[Originally posted by knighthawk on 2008-08-24 14:36:29-07]

Hi Phil, thanks for looking into it. I still am facing issues, so I decided to test it out. I clicked four photos today with rotation 0, 90, 180 and 270 CW. I'm using Ubuntu 8.04, and the OS automatically rotated the 90, and 270 images while displaying (but if previewed when uploading to a Website, I get to see the original orientation). The 180 degree image, on the other hand was left as is.

Problem 1: When I ran exiftool with the -n option, the 180 degree image returned the Orientation option as "1" instead of a "3".

Problem 2: I got the following error when I ran the script:
Code:
Use of uninitialized value in hash element at jpegtran.plx line 18.
Use of uninitialized value in concatenation (.) or string at jpegtran.plx line 18.
Problem 3: How do I handle the option "1" (default, no rotate)

Sorry to ask again, I'm a novice to Perl. I have listed the code below.

Code:
#! usr/bin/perl

use strict;
use warnings;

use Image::ExifTool qw(:Public);
my $exifTool = new Image::ExifTool;

my %rot_args = (
   2 => '-flip horizontal',
   3 => '-rotate 180',
   4 => '-flip vertical',
   5 => '-transpose',
   6 => '-rotate 90',
   7 => '-transverse',
   8 => '-rotate 270' );
my $rotation = $exifTool->GetValue('Orientation', 'ValueConv');
my $command = "jpegtran -copy all $rot_args{$rotation} IMG_7885.JPG > IMG_7885_2.JPG";
exec($command);

Archive

[Originally posted by exiftool on 2008-08-25 10:42:04-07]

The 180 degree image was left as-is because the
Orientation was set to "Normal".

The script needs to check for null values.  Maybe something
like this...

Code:
...
my $rotation = $exifTool->GetValue('Orientation', 'ValueConv');
unless (defined $rotation and defined $rot_args{$rotation}) {
    print "Nothing to do\n";
    exit 0;
}
my $command = "jpegtran -copy all $rot_args{$rotation} IMG_7885.JPG > IMG_7885_2.JPG";
...

You don't rotation an image if the Orientation is "Normal", so
you should do nothing if the value is 1.  If your camera uses
this value when the image is rotated by 180 degrees, it is because
the camera doesn't know when it is upside down.

- Phil

Archive

[Originally posted by knighthawk on 2008-08-26 10:28:33-07]

Thanks Phil,

I just realized nobody rotates a camera 180 degrees to take out photos! :-)

I have another query, I managed to successfully rotate an image by 90 degrees. In the new image, the orientation has changed to this:

Code:
Orientation                     : Rotate 270 CW

Is it advisable to clear the tag value, or reset it to something else?

Archive

[Originally posted by exiftool on 2008-08-26 10:37:55-07]

I don't have enough information to answer the question.
But if the image is the correct orientation, the tag
(in theory) should be set to Horizontal (Normal).

- Phil