Image orientation

Hi!
Is there a script to determine image orientation?

Like:

  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

We need more to go on to understand what you’re asking. Are you asking for access to an attribute tag that has that information? Or are you asking for the computer to recognize the image contents and discern the proper orientation, including things like mirroring, like a human could attempt to do? Basically, strong AI?

I have a script that convert any image to widescreen:

on run {input, parameters}
	repeat with this_file in input
		set this_file to (this_file as text)
		tell application "Image Events"
			-- start the Image Events application
			launch
			-- open the image file
			set this_image to open this_file
			-- get dimensions of the image
			copy dimensions of this_image to {W, H}
			-- determine the letterbox area
			set crop_W to W
			-- calcluate the 16:9 proportions
			set crop_H to (W * 9) / 16
			-- perform action
			crop this_image to dimensions {crop_W, crop_H}
			-- save the changes
			save this_image with icon
			-- purge the open image data
			
		end tell
		
	end repeat
end run

And I want this script to skip vertical orientation by dimension and by EXIF tags like: Flipped Horizontally, Rotated etc.
So this script will process only horizontal images…

Simply restricting it to only acting on images in landscape orientation is certainly easy. I haven’t looked into the EXIF tags yet.

on run {input, parameters}
	repeat with this_file in input
		set this_file to (this_file as text)
		tell application "Image Events"
			-- start the Image Events application
			launch
			-- open the image file
			set this_image to open this_file
			-- get dimensions of the image
			copy dimensions of this_image to {W, H}
			-- only act on landscape image
			if W > H then
				-- determine the letterbox area
				set crop_W to W
				-- calcluate the 16:9 proportions
				set crop_H to (W * 9) / 16
				-- perform action
				crop this_image to dimensions {crop_W, crop_H}
				-- save the changes
				save this_image with icon
				-- purge the open image data
			end if
		end tell
	end repeat
end run

I don’t think EXIF is what’s meant. The numbers in post #1 correspond to those shown in the “General” and “TIFF” tabs of Preview’s Inspector window when the orientation of a displayed image is changed. If the image is saved with a new orientation, that becomes the new “1 (normal)” when the image is opened again.

Ah. I hadn’t recognized those as being from a particular type of tag, I thought it was just a list of possibilities he was trying to recognize.

So it sounds like he wants to skip portrait orientation, but simply skipping based on h>w would miss images that are only portrait because they are tagged to display in a different orientation than the the default pixel order.

Sounds easy enough, I’ll take a look at accessing those tags when I have a chance.

  • Tom.

Yeah, you right. Thanks in advance…

I haven’t had a chance to do any testing yet, but I think this should get you close:

on run {input, parameters}
	repeat with currentFile in input
		set posixFilePath to the quoted form of (POSIX path of currentFile)
		set orientationFlag to (do shell script "mdls " & theFile & " -name kMDItemOrientation")
		set theOrientation to text ((offset of "= " in orientationFlag) + 2) through end of orientationFlag as number
		set currentFile to (currentFile as text)
		set rotatedOrientations to "5,6,7,8"
		tell application "Image Events"
			-- start the Image Events application
			launch
			-- open the image file
			set thisImage to open currentFile
			-- get dimensions of the image
			copy dimensions of thisImage to {W, H}
			-- only act on landscape image
			set cropIt to false
			if theOrientation is not in rotatedOrientations then
				if W > H then
					set cropIt to true
					-- determine the letterbox area
					set cropW to W
					-- calcluate the 16:9 proportions
					set cropH to (W * 9) / 16
				end if
			else if theOrientation is in rotatedOrientations then
				if H > W then
					set cropIt to true
					set cropW to H * 9 / 16
					set cropH to H
				end if
				-- perform action
				if cropIt is true then
					crop thisImage to dimensions {cropW, cropH}
					-- save the changes
					save thisImage with icon
				end if
				-- purge the open image data
			end if
		end tell
	end repeat
end run

Thanks, but get an error:

The action “Run AppleScript” encountered an error.

Try to edit the instruction :

set posixFilePath to the quoted form of (POSIX path of currentFile)

as

set theFile to the quoted form of (POSIX path of currentFile)

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 2 février 2017 20:52:25