Recognising portrait orientation in JPG files

Some programs, e.g. Adobe Bridge CS3, seem to be able to recognise whether a JPG file is of a photo in portrait or landscape mode and display them as appropriate. Other programs, in particular the Screen Saver, don't seem to have this ability. I therefore use ImageJ to rotate files with portrait format and then save them again, so that the Screen Saver also displays them with the correct orientation.

I thought I should be able to automate this process for selected folders using Applescript. However, looking at the info displayed by the Finder for JPG files, I don't see any difference in the properties between rotated and non-rotated portrait layout photos.

Does anyone know how this property is stored? Is there more EXIF data hidden away somewhere?

Intel iMac, Mac OS X (10.5.7), iPod classic 160GB; QNAP NAS

Posted on Jul 13, 2009 11:51 AM

Reply
7 replies

Jul 13, 2009 2:01 PM in response to Eric Sprigg

You should be able to work it out via Image Events.

set theOriginal to (choose file)

tell application "Image Events"
set theImage to open theOriginal
set {x, y} to theImage's dimensions

if x > y then -- image is landscape...
rotate theImage to angle -90 -- so rotate it
save theImage in (theOriginal as text) & ".new.jpg" -- and save a new version
end if
end tell


Of course, it's hard to automatically work out whether to rotate clockwise or counter-clockwise. That's up to you to decide 🙂

Jul 13, 2009 2:20 PM in response to Eric Sprigg

With some JPEG pictures, if you open them in Preview.app and select Tools > Inspector, then More Info > General, you will get information about their orientation, for example: "1 (Normal)", "6 (Rotated 90° CW)", "8 (Rotated 90° CCW)", etc.

If no such information exists, maybe you can do as Camelot suggested and just compare (under the General Info tab) the image size: “800 x 600 pixels” is most likely a landscape, whereas “600 x 800 pixels” would be a portrait.

With AppleScript, you can use GUI Scripting to get these informations from Preview.app.

Jul 13, 2009 3:09 PM in response to Pierre L.

Here is just a first draft of a script that can get some useful information from Preview.app:

tell application "Preview" to activate
tell application "System Events" to tell process "Preview"
if title of window 1 is not in {"General Info", "More Info", "Keywords"} then keystroke "i" using command down -- to open the Inspector
click radio button 2 of radio group 1 of window 1 -- More Info
click radio button "General" of tab group 1 of window 1 -- General
if (row 1 of table 1 of scroll area 1 of group 1 of window "More Info" whose value of text field 1 is "Orientation") exists then
get value of attribute "AXValue" of text field 2 of (row 1 of table 1 of scroll area 1 of group 1 of window "More Info" whose value of text field 1 is "Orientation") --> "1 (Normal)", "6 (Rotated 90° CW)", "8 (Rotated 90° CW)", etc. for example
else
click radio button 1 of radio group 1 of window 1 -- General Info
return value of UI element 1 of window 1 whose value contains "pixels" and value does not contain "inch" --> "800 x 600 pixels" for example
end if
end tell

It must be possible to do much better.

Jul 14, 2009 1:39 PM in response to Camelot

Thanks for the help.

[quote]Of course, it's hard to automatically work out whether to rotate clockwise or counter-clockwise. That's up to you to decide 🙂 [/quote]
True. I'd thought about that. I've also noticed that I seem to hold the camera differently from other people who take photos with it in portrait mode. I usually have to rotate counter-clockwise, others clockwise.

Strangely, I don't think I've ever seen Adobe Bridge get the rotation wrong, so I have wondered whether the camera (a simple Canon iXus 60) has some sensor which saves the orientation somewhere in the EXIF data.

Jul 14, 2009 2:10 PM in response to Pierre L.

Thanks for these useful tips. Comparing photos store by my Canon iXus 60 with those from a friend's Casio camera, I see in the Inspector More Info data that the Canon seems to store more information about the photo, in particular it has (for a portrait photo) a field
Orientation 8 (Rotated 90° CCW) (for example)
whereas the Casio for a portrait photo only shows
Orientation 1 (normal)
(which is what the Canon also stores for landscape photos).

Experimenting with the Canon I find that it distinguishes, for portrait photos,
Orientation 8 (Rotated 90° CCW)
Orientation 6 (Rotated 90° CW)
but returns
Orientation 1 (Normal)
for both a normal landscape photo and one taken upside down.
Holding he camera at about 45° brought Orientation 1, in one case, and Orientation 6 in another.
However, for normal use with my Canon, that seems to be the information I need to do what I want.

Thanks too for the sample script. Being an absolute beginner with Applescript, that is a very useful starting point for me 🙂

Jul 14, 2009 2:21 PM in response to Eric Sprigg

Comparing photos store by my Canon iXus 60 with those from a friend's Casio camera, I see in the Inspector More Info data that the Canon seems to store more information about the photo, in particular it has (for a portrait photo) a field... [snip]


That's because the iXus has an orientations sensor within it that detects the orientation of the camera and can record that alongside the image. The Casio (like most cameras) doesn't have an orientation sensor, so it is unable to determine which way up the image was taken.

returns Orientation 1 (Normal) for both a normal landscape photo and one taken upside down.


The sensor is likely detecting offset from horizontal. Since right-side-up and upside-down are both, essentially, horizontal, they're both recorded the same way (few people take pictures upside down 😉 )

Holding he camera at about 45° brought Orientation 1, in one case, and Orientation 6 in another.


That's probably because you weren't quite at 45 degrees on the first shot.

As for the script, since that data is stored in the EXIF data, it should be possible to extract it via Image Events, so a slightly puffed-up version of my script should be able to orient it correctly.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Recognising portrait orientation in JPG files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.