images are rotated 90 degrees in Rich Text

I drag a JPG photo image into a Rich Text (RTF) file. Usually it works fine. Often however, the image is rotated 90 degrees. If I import this image into DevonThink directly (not into an RTF) then it displays correctly. If I then drag that image into the RTF it again is 90 degrees rotated.

There are no commands for modifying images inside an RTF that I can find (though I just saw there is perhaps now a scaling slider on the side for an image).

Any workarounds? Is there a place to report this as a bug?

Thanks.

Which version of Mac OS X do you use and how did you rotate the images? Did the camera/scanner automatically rotate them?

Because the most likely reason for the issue is that the images use a rotation flag not supported by the rich text engine of Mac OS X but supported by Preview and DEVONthink (via the ImageKit framework).

Possible workarounds coming to my mind are to open the image in Preview, to rotate and save the image. Or to “really” rotate the images, e.g. DEVONthink Pro’s Scripts > Images > …

I’ve figured this out and I’m reporting back in case its useful to anyone.

It is indeed the case that the rich text engine used in DevonThink is ignoring the orientation flag in the EXIF meta data of the photo image. (DevonThink is in the minority here, because Finder, Preview, TextEdit all obey the orientation flag.)

There are various workarounds. One is to open the image in Preview, make some slight change (maybe rotate and unrotate for example) and save; Preview seems to always save photos with “normal unrotated” orientation.

For myself, I wrote an AppleScript which makes a low resolution version of a photo and also rotates the photo if necessary to be in “normal unrotated” orientation. The photo then correctly imports into a DevonThink rich text document.

Here is the AppleScript, in case you are interested:


-- Summary:  Makes a low res version of a photo image (WARNING: deletes previous lowres version)
-- and rotates the image according to the EXIF orientation flag.
--
-- Why:
-- 1.  To make a smaller version (file size and screen size) for putting into
-- RTF notes like DevonThink, or for sending via email, or posting online.
-- 2.  Because DevonThink doesn't obey the EXIF orientation flag, we rotate the image.
--
-- What this does:
-- Has a predefined size in pixels to scale the image to (maximum width or height);
-- rotates the image according to EXIF orientation flag;
-- uses high compression / low quality in JPEG "save as" command;
-- deletes any previous lowres version (WARNING!);
-- saves to a new file with "lowres" appended to the file name.
--
-- Preparation
-- Save this script as an application.
-- Install exiftool.
-- (Image Events is an application that is pre-installed by Apple.)
-- 
-- Usage: Two ways:  
-- 1.  Drag file(s) onto this script application.
-- 2.  Select files in Finder and run this script in Script Editor (this way allows for debugging).
--
-- History:
-- By Erik Neumann July 2010
-- I started from the script given in this article:
-- "Drag-and-drop script to quickly resize any image" Sep 28, '04 by robg (Rob Griffiths) 
--  at http://www.macosxhints.com/article.php?story=2004092804461334
--
-- Resources:
-- 
-- JPEG Rotation and EXIF Orientation
-- http://www.impulseadventure.com/photo/exif-orientation.html
-- The EXIF Orientation Flag Values are:
-- 1 = not rotated (normal wide landscape orientation)
-- 3 = 180 degree rotation (upside down)
-- 6 = rotated 90 degree counter clockwise
-- 8 = rotated 90 degree clockwise
-- (2, 4, 5, 7 are similar to above but are mirror image as well)
-- The Orientation flag is stored in the JPEG APP1 marker under EXIF IFD0.
--
-- exiftool by Phil Harvey
-- http://www.sno.phy.queensu.ca/~phil/exiftool/
-- "ExifTool is a platform-independent Perl library plus a command-line 
-- application for reading, writing and editing meta information in a wide 
-- variety of files."
-- You can simply give the command "exiftool filename" in Terminal to
-- see most of the meta data in the file.
-- 
-- AppleScript Language Guide
-- http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf?q=applescript
--
-- image test
-- An AppleScript by Erik Neumann that lists information about an image using
-- the Image Events application, exiftool, and Finder.
--

-- this happens when you click "run" in Script Editor
on run
	-- (it seems necessary to copy the selection here, unclear why)
	tell application "Finder" to copy selection to theSelected
	repeat with this_item in theSelected
		rescale_and_save(this_item as alias) of me
	end repeat
end run

-- this happens when you drag files onto the application
on open some_items
	repeat with this_item in some_items
		rescale_and_save(this_item as alias) of me
	end repeat
end open


on rescale_and_save(this_item)
	try
		log "in rescale_and_save"
		set filename to POSIX path of this_item
		log filename
		set orientation to do shell script "exiftool -n -Orientation -S " & filename
		log orientation
		
		tell application "Image Events"
			launch
			-- Here is where to change the maximum width/height of the image.
			set the target_size to 600
			-- Note:  the following "open this_item" statement behaves differently 
			-- depending on whether this_item is an alias or not.
			-- if this_item IS an alias, then Image Events opens the file
			-- if this_item IS NOT an alias, then file opens in Preview (presumably
			-- the Finder is handling the open command in that case).
			-- Conclusion:  a non-alias file retains information about the object
			-- that created it (ie. the Finder)?
			set this_image to open this_item
			set typ to this_image's file type
			-- Note: dimensions are unaffected by rotation and scale commands;
			-- presumably those commands modify an in-memory new version of the file.
			copy dimensions of this_image to {current_width, current_height}
			log "width " & (current_width as string) & " height " & (current_height as string)
			-- rotate the image to an unrotated state if necessary.
			if orientation is "Orientation: 6" then
				rotate this_image to angle 90
			else if orientation is "Orientation: 8" then
				rotate this_image to angle -90
			else if orientation is "Orientation: 3" then
				rotate this_image to angle 180
			end if
			-- The scale command in Image Events dictionary says: "scale using a max width/length"
			-- so presumably the largest dimension of height or width will become this amount.
			scale this_image to size target_size
			set theName to name of this_item
			set theType to name extension of this_item
			-- trimName is the file name without extension
			set trimName to text 1 thru -((length of theType) + 2) of theName
			tell application "Finder" to set new_item to ¬
				(container of this_item as string) & trimName & ".lowres." & theType
			if exists file new_item then
				delete file new_item
			end if
			save this_image in new_item as typ with compression level high
			tell application "Finder" to set newFileName to POSIX path of new_item
			-- fix the orientation in this new file to be non-rotated
			if orientation is "Orientation: 6" or orientation is "Orientation: 8" or orientation is "Orientation: 3" then
				do shell script "exiftool -overwrite_original -n -Orientation=1 " & newFileName
			end if
			log newFileName
		end tell
	on error errText number errNum
		set msg to "low res script" & return ¬
			& "error " & errText & " " & (errNum as text) & return ¬
			& "File is " & filename
		log msg
		display dialog msg
	end try
end rescale_and_save

-- end of low res AppleScript