Rotating photos automatically

Stuff you need to do that just sneaks up on you. I’m sure this has happened to the most of us. This article is about one of those that is really high on the list of the “things to fix” and can involve quite a bit of data. Which usually means that I try to put the task off for as long as possible since manual intervention works just as well, and large amounts of data make for a big impact when things go wrong. “Think it over, before trying to fix it”, is something I’ve learned to memorize over the years as an IT professional. Only this time I’m talking about rotating digital photo’s on my Mac, so they can be edited as they were taken. There is usually a lot of them, and you must examine every photo, because not all photos need to be rotated.

My wife jumped for joy when she found out that Mac OS X’s Finder rotates landscape to portrait automatically, only to be really disappointed when she found out that the album creation software didn’t. Which means that Finder displays them correctly, but doesn’t alter them so that other – less intelligent – software can use them as they were displayed. Bummer! Back to the dark ages of my old PC, where every chore feels like manual labor or is fixed with expensive software.

I’ve looked for a long time for a simple and small solution to this problem, because I cannot imagine that I’m the only soul in the world with this problem. So some 3 years ago I found jhead; a small program that can be used to rotate jpeg images automatically and without losing image quality. This doesn’t mean that there is no other solution, it means that I probably suck at google and got tired of searching and using stuff that didn’t do what I expected it to do for me.

Meet Jhead. Unfortunately, Jhead isn’t an easy solution for the Mac. It’s a command line tool, depending on the jpegtran library. Jpegtran is available only as sourcecode. So you need to compile that and to do that, you need a compiler. A previous version of Xcode is available on Apples’ developer site for free, which seems appropiate if you will use the compiler only once or twice. I took the time to compile jpegtran. You can download the binary files including manual pages and header files right here. If you are security-minded you might want to think twice about downloading and installing binaries from a site like this. It just might destroy every file on your harddrive. For me, Jpegtran compiled without problems, so you should be able to simply run ‘./configure && make && make install’, if you have a compiler installed. But that may differ from version to version. Jhead is available as both sourcecode and precompiled binary on the official site.


If you use the archive from this site: Open the Terminal app, become root by typing ‘sudo su -’ and extract the tar.gz like this:

tar -C / -z -x -v -f jpegtrans.tar.gz

Which should install everything related to jpegtran.

If that is done you can log out as root by pressing [ctrl]d

Next, download jhead and install that in a directory named ‘bin’ in your home-directory. This will make jhead available to you exclusively, more about that later. You could do that with the following commands:

mkdir ~/bin
chmod 755 ~/bin
cp jhead ~/bin

The following shellscript calls jhead for every file that ends in .jpg or .jpeg, and depending on the information in the jpeg-file, Jhead may decide (not) to rotate the image:


find . -type f | egrep -i '\.jp[g|eg]$' | while read l
        jhead -autorot "$l"

Copy the shellscript in a file and name this file ‘rotate’ (no extension). Then, copy it to a directory named ‘bin’ in your home directory. Make it executable by using ‘chmod +x rotate’. Next, create a file in your home directory named ‘.bash_profile’ and add the following lines to it:

export PATH

This should allow the command line to find the rotate script in the bin directory in your home directory.
Back in terminal app, hit [cmd]T, which should bring a new shell. To check if the script is installed properly, type ‘which rotate’ and you should see something like ‘/Users/[your-loginname]/bin/rotate’. The same applies to ‘which jhead’.

That’s it! You are ready to rotate every photo automatically! Simply ‘cd’ to the directory containing your non-rotated files and type ‘rotate’.


Automatically rotating photos as described here works perfectly for photos created with my camera, a Nikon, but does not necessarily work for photos from any camera. This is likely depending on EXIF information, which – I assume – is missing or misinterpreted by Jhead.

The rotate script will attempt to rotate every non-rotated jpeg-image in the directory you start it, and every directory below that. Beware that if you type ‘cd’ and then ‘rotate’, rotate will traverse your whole home-directory.

Installing the jhead binary in your home-directory is likely to prevent other users on your computersystem from using it, unless you alter the access rights on your home-directory which may be undesirable. To allow all users on the computersystem to use jhead, it could be installed in /usr/local/bin. I didn’t do this because I had no need for that.

Since the method is 100% unix shellscript, this should work on pretty much any unix-flavor, including Ubuntu and FreeBSD. For Mac OS: The jhead binary from the official site is a univeral binary, including the PPC architecture. Which reminds me: Jhead is also available for Windows! This is how I use Jhead on my PC, as a MS-DOS batch file:

PATH=%PATH%;C:\program files\jpegtools
for /R %%a in (*.jpg) do "c:\Program Files\jhead.exe" -autorot "%%a"


This entry was posted in Photography and tagged , , . Bookmark the permalink.