Determining Image Orientation?

Hello all,
I am new to this forum, so it is quite likely this question has been dealt with previous… if so please point me to thread…

I am trying to devise a more efficient image workflow and would like a script which can determine an image’s orientation. ie. portrait or landscape. Then split the two types into seperate folders so as to be batched by photoshop(CS2) actions…

thanks for your help,

Browser: Safari 416.12
Operating System: Mac OS X (10.4)


Using Image Events to get the dimensions of the image will do the trick:

set a to choose file
tell application "Image Events"
	close images --This line is not always necessary, but helps keep things clean.
	set b to open (a as alias)
	set {wid, hit} to b's dimensions
	close b
end tell
if wid < hit then
	display dialog "This sucker is portrait"
	display dialog "This hummer is landscape"
end if

Sorry about the strange dialogs; I wrote this a few weeks ago while I was messing around on another project.

Hi Paul. Craig’s excellent advice should have resolved the image orientation side of your question - and I wondered if adding an example that sorts image files into folders might also help.

Before you run the script below, create 3 folders (or decide which existing ones you’ll use) for: original images (mixed format), landscape images and portrait images. The script should move any image files from the originals folder to either the landscape or portrait folder, as appropriate. (Square images will be treated as portrait. If the originals folder also contains non-image files or sub-folders, these should remain untouched.)

property originalFolder : missing value
property landscapeFolder : missing value
property portraitFolder : missing value

to chooseFolder(t)
	choose folder with prompt "Choose a folder to contain your " & t & " images:"
end chooseFolder

to chooseFolders()
	set {originalFolder, landscapeFolder, portraitFolder} to ¬
		{chooseFolder("original"), chooseFolder("landscape"), chooseFolder("portrait")}
end chooseFolders

on landscape(f)
	tell application "Image Events" to tell (open f)
		tell (get dimensions) to if (count) is 2 then
			set v to item 1 > item 2
			set v to missing value
		end if
	end tell
end landscape

if originalFolder is missing value then chooseFolders()
set landscapeList to {}
set portraitList to {}
launch application "Image Events"

tell application "Finder"
	repeat with f in (get document files of originalFolder)
		tell my landscape(f as Unicode text) to if it is true then
			set landscapeList's end to f
		else if it is false then
			set portraitList's end to f
		end if
	end repeat
	move landscapeList to landscapeFolder
	move portraitList to portraitFolder
end tell

Hi. Sorry to put a damper on this very reasonable idea, but it doesn’t always work. I was sent an image file recently that’s in portrait orientation, but its dimensions are returned by Image Events as {3072.0, 2040.0}. Getting the information for the file in the Finder shows the same dimensions, although the picture’s correctly orientated in the preview. The person who sent me the file told me that all digital camera images are horizontal, but contain additional “EXIF data” that give the pictures’ orientations. Unhelpfully, Image Events doesn’t seem to be aware of this.

That said, the image I received was a raw image file. It may be that the dimensions of processed images are adjusted by the processing applications and that this cautionary note isn’t necessary. I’m not an expert in these matters. :frowning:

Hi Nigel Garvey,
You (and others) might be interested in this Mac OS X Hints article: Mac OS X Hints - A community-built collection of OS X hints

iMagine Photo, which is much like “Image Events” but with more features can return you exif orientation information from the image file if it contains that information. The following script will leave in the results section of Script Editor if you click on the results tab before you run the script.

tell application "iMagine Photo"
	set thisFile to choose file with prompt "Choose an image file containing exif data:" without invisibles
	set thisImporter to import graphic thisFile
	set exifInfo to the exif data of thisImporter
	close thisImporter
end tell

Funnily enough the orientation exif information is called orientation, and the usual result is “Normal”. The possible orientations are:

  1. Normal
  2. Flipped Horizontally
  3. Rotated 180
  4. Flipped Vertically
  5. Rotated 90 clockwise, then flipped horizontally
  6. Rotated 90 anticlockwise
  7. Rotated 90 anticlockwise, then flipped horizontally
  8. Rotate 90 clockwise


Thanks to both Qwerty and Kevin for the information, which I personally found very interesting.

Kevin. iMagine Photo seems a handy little app from the minimum time I’ve had with it so far. But it still doesn’t work in the current context with the CRW file I was talking about above. It doesn’t return anything when asked for the ‘exif data’ of the importer. It seems to be OK with GIFs and JPEGs, returning an empty list they don’t have any EXIF data.

You could also add the height/width check to your PhotoShop script. That way you run one script on ALL files and the script will sort out which ones go throught he landscape process and which ones go through the portrait process. The script could call out two separate sub-routines if the process is that different for the two types of files. They could even save in the end to different folders if you need the two types of files sorted out. Anyway, I used this check in a Photoshop script I wrote. It would go in after you open an image to process:

if width of current document is greater than the height of current document then
			--Do the landscape steps or subroutine
			--Do the portrait steps or subroutine
		end if

Model: Mac G5
Operating System: Mac OS X (10.3.9)

Thanks heaps to everyone who replied… it has given me a lot to go off… I have always been attracted to scripting as a process, but way to many projects and not enough time has made the learning curve a bit to steep… anyway, thanks again for everyones help…