create drop on app for lossless rotation of a picture using exif tag Orientation

Started by mkalk, August 10, 2011, 11:15:36 AM

Previous topic - Next topic

mkalk

At the risk of stating the obvious (although I've been looking extensively for a tool before constructing this solution myself) I would like to share how I went about creating a minimalistic tool to rotate my pictures from the Finder on my mac.

Let's start with the problem I was trying to solve.
I own a point and click camera that doesn't know which way it is held when taking the picture (i.e. whether it is held horizontally, or in portrait mode either at a 90 or 270 degree angle).
When copying my pictures to my mac and subsequently to my iPad, I will see the portrait picture the wrong way (in a 90 or 270 degree angle), which has been irritating me for quite some time.
After an extensive search of how to solve this issue I came to two conclusions:
- I want to solve this by changing the Orientation exif tag, rather than rotating the picture in an image editor and losing quality
- The only tool I found that can actually edit the Orientation tag (at least on the mac) is exiftool

The only drawback of exiftool is that it's a command line tool and I normally look at my pictures in Finder or in Picasa for the obvious reason that looking at a picture from the command line is impossible (at least to my knowledge). So now I was left with the challenge of creating a reasonable workflow to flip through my pictures and easily change the Orientation tag whenever I come across a picture in portrait orientation.

So that's the problem. Now for the solution.
To solve this problem I took the following steps:

1. Install exiftool
Since you're on this forum I assume you already have it installed or know how to do it. If not, please visit the exiftool site: http://www.exiftool.org/

2. create a script to execute the right exiftool command.
This script will accept the filename as an argument.
The code of this script is as follows for the 90 degree orientation
#!/bin/sh
exiftool -Orientation=6 -n -overwrite_original_in_place $1


And this is the code for the 270 degree orientation
#!/bin/sh
exiftool -Orientation=8 -n -overwrite_original_in_place $1


I saved these files as rotate90 and rotate270 respectively

3. make the scripts executable
In order for the mac to recognize this file as an executable you have to make it an executable using the following command (in the terminal)
sudo chmod +x rotate90
and
sudo chmod +x rotate270

4. download and install dropscript
This is a simple app that will allow you to create an app from an executable script
You can find this app at the following website: http://www.wsanchez.net/software/
Download the dmg for this app and place the app on the desktop.
After placing the app on my desktop I simply dragged (from the finder) the executable file to the dropscript app on the desktop.
This creates a Droprotate90.app and Droprotate270.app in the same directory

5. Place the apps on the desktop and drag and drop your pictures on it
Place the apps in a convenient corner of your desktop so you can still see them when you open your finder window.
I like to browse through pictures in the cover flow mode of finder and whenever I come across a portrait picture, I drag it to the appropriate app.
What happens then is that exiftool changes the Orientation exif tag, which subsequently convinces Finder to show the picture in the correct orientation.
And of course this is also the case when you look at the picture in Picasa, on your iPad or in any other viewer that can handle exif tags.

I hope this helps all of you being annoyed by the same problem (and owning a mac).
Enjoy  ;)


Phil Harvey

Thanks for this.  At some point, with your permission, I may like to edit this and move it to the "Solutions" section of the web site.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

mkalk

Phil,
I'm glad you like it.
And of course no problem if you want to edit and move it. I assume I will be notified when it's moved so I can keep track of the replies. You never know what clever minds are out there that may have improvement suggestions. :)
Thanks for the great tool

StarGeek

Quote from: mkalk on August 10, 2011, 11:15:36 AM
- I want to solve this by changing the Orientation exif tag, rather than rotating the picture in an image editor and losing quality

In case you didn't know, it is possible to rotate jpgs without losing any quality.  A quick google pulled up a program called PhotoToolCM (scroll down a bit) for the Mac or you could always look for the original command line program called JpegTran (Wiki entry on libjpeg library, which tells a bit about it).

I often rotate my jpegs so their orientation is correct in windows using IrfanView and I've tested it by rotating an image 90 degrees four times in a row.  The end file was a binary match to the original
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Yes, but remember that lossless rotation is possible only if the image dimensions are a multiple of 8.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

mkalk

Stargeek,
Thanks for the tip. This PhotoToolCM looks interesting, although I'm still hesitant to actually rotate the pixels of my jpeg's. But all the other listed features seem interesting.
One question though. Do you know if this tool is still current? It seems the site is rather old (2005?) and I didn't find a link to download the tool (only the screenshots).

StarGeek

Quote from: mkalk on August 12, 2011, 08:23:29 AM
One question though. Do you know if this tool is still current? It seems the site is rather old (2005?) and I didn't find a link to download the tool (only the screenshots).

My bad then.  I don't use a mac and assumed that the "download" link provided downloaded the program, not the screen shots (I can't unpack a .sit).  I would think there would be some program out there for the mac, though.  I use Irfanview and Imatch on windows and both have the ability to do this.

Quote from: Phil Harvey on August 12, 2011, 07:38:06 AM
Yes, but remember that lossless rotation is possible only if the image dimensions are a multiple of 8.

I must have forgotten that.  But then, checking my camera pictures over the past 12 years, they've all taken pictures in multiples of 8 anyway.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

JanK

I want to rotate my pictures with exiftool too but want to save many clerical work. Entering Terminal and typing the exiftool command for every picture is not an efficient solution. So the way with a droplet is more efficient. Better would be a command which I can execute by the context menu of the mouse (right click) or the toolbar.
Phil said that he want to store the description from mkalk in the solutions of the website.

So my questions:

  • Where can I find the article in the solutions?
  • With which tool can I generate the script and in what should I save it?
  • Is there a way to execute the command for exiftool for a marked picture in finder by a button in the toolbar? In the toolbar I can store programs so it may be solution to rotate marked pictures by clicking a button in the finder-toolbar.
  • Same way as 3. but an entry in context menu of the mouse (right click). I found something like that in the deep of the forum but don't know where.
-Mac OSX Mountain Lion-

Phil Harvey

Quote from: JanK on January 02, 2012, 09:00:08 AM
1. Where can I find the article in the solutions?

Sorry, but I removed the Solutions section of the forum because it wasn't being used.  Solutions are now posted in this section.

Quote2. With which tool can I generate the script and in what should I save it?

On the Mac, AppleScript is probably the way to go.  You edit these scripts with the AppleScript Utility.  There are a few example scripts that may help you get started under "Mac OS X" in the "Related Utilities" section of the ExifTool home page.

There are also automator actions that may be created in OS X.

It is certainly easy to make a drag-and-drop utility this way, but I'm not sure about a finder toolbar or contextual menu function.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

JanK

Oh my good.
QuoteTell application "Netscape Communicator" to OpenURL "www.CNN.com"
Hey do I speak to a human? I need complete human sentences to work with AppleScript? Where is the Pro-Mode? I did not work with Apple Script before but with other Programming Languages. Is this really effective to work with Apple Script? Also on longer scripts? It is very hard to understand scripts when there is code written like a book.


Ok because I need some script to rotate my pictures I use this in Apple Script:


on open filelist
repeat with i in filelist
do shell script "exiftool -Orientation#=6 -overwrite_original -P " & quoted form of POSIX path of i
end repeat
end open


Save this as a program in Apple Script. Now you can Drag n Drop files on the generated file maybe on the desktop. Dropscript or something else is not needed. Not very good but useful at the moment.

I have some question to the result:
I rotate a file with Width x Height = 3264 x 2448 captured in portrait and no automatic Rotation so the Rotation tag was set to Horizontal.
In the Finder the Thumbnail and the opened picture displays wrong. So I set the Rotation to 90CW. Picture (when I open it with Preview) and Thumbnail is in the right orientation. Now I set the Rotation back to Horizontal. The thumbnail in Finder is in the correct orientation (portrait). And the picture is in the orientation it was before. Why is the thumbnail not in the way it was before?
Edit: I found the answer. Because of the -P option OSX think that the file was not modified so it don't update the thumbnail cache in OSX. So delete the thumbnail cache of OSX or don't use -P

The ThumbnailOffset before the rotation (Horizontal) was:
[IFD1]          ThumbnailOffset                 : 5120

and after the rotation (90CW)
[IFD1]          ThumbnailOffset                 : 3382

and after the rotation back to Horizontal
[IFD1]          ThumbnailOffset                 : 3382


everything else stays the same except the Orientation Tag.
What is the ThumbnailOffset and why was it changed? ANd why was it not changed back to the value at the beginning?
-Mac OSX Mountain Lion-

Phil Harvey

It sounds like you're getting along well and solving some problems.  I'm not an AppleScript programmer either, and it confuses me too.

The ThumbnailOffset is just the byte offset in the EXIF information where the thumbnail image is stored.  It isn't significant.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

pb

It may be in this very forum that I read that all known cameras produce jpegs with dimensions which are a multiple of 8.  I think this is more of an issue for lossless cropping than lossless rotation. 

Also, regarding Irfanview, note that jpegtran and therefore Irfanview has an option to improve the huffman coding of the data (called "optimize").  What this means is that the image is compressed better, but no additional data is lost.  If this option is left checked, 360 degrees of rotation will not result in a bit for bit identical file, although the reconstructed image will be bit for bit identical, assuming everybody implemented everything correctly.

--peter

Quote from: StarGeek on August 13, 2011, 02:31:14 AM
Quote from: mkalk on August 12, 2011, 08:23:29 AM
One question though. Do you know if this tool is still current? It seems the site is rather old (2005?) and I didn't find a link to download the tool (only the screenshots).

My bad then.  I don't use a mac and assumed that the "download" link provided downloaded the program, not the screen shots (I can't unpack a .sit).  I would think there would be some program out there for the mac, though.  I use Irfanview and Imatch on windows and both have the ability to do this.

Quote from: Phil Harvey on August 12, 2011, 07:38:06 AM
Yes, but remember that lossless rotation is possible only if the image dimensions are a multiple of 8.

I must have forgotten that.  But then, checking my camera pictures over the past 12 years, they've all taken pictures in multiples of 8 anyway.