Thread

Posted on Fri Aug 22 16:10:34 2008 by knighthawk
Detecting Orientation
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:
my $rotation?
Direct Responses: 8661 | Write a response
Posted on Sun Aug 24 14:34:25 2008 by exiftool in response to 8654
Re: Detecting Orientation
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:

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
Direct Responses: 8664 | Write a response
Posted on Sun Aug 24 17:36:29 2008 by knighthawk in response to 8661
Re: Detecting Orientation
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:
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.
#! 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);
Direct Responses: 8666 | Write a response
Posted on Mon Aug 25 13:42:04 2008 by exiftool in response to 8664
Re: Detecting Orientation
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...

... 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
Direct Responses: 8672 | Write a response
Posted on Tue Aug 26 13:28:33 2008 by knighthawk in response to 8666
Re: Detecting Orientation
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:
Orientation : Rotate 270 CW
Is it advisable to clear the tag value, or reset it to something else?
Direct Responses: 8673 | Write a response
Posted on Tue Aug 26 13:37:55 2008 by exiftool in response to 8672
Re: Detecting Orientation
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
Write a response