How To: Automate HDR on Linux - Part 1

The first step to automating HDR processing is to get your images broken up into sets that you can work with. After going out to shoot pictures I can easily end up with 100 5 shot sets of images. That’s 100 HDR images to produce from 500 source images. File management can get to be a challenge and trying to do it all by hand is very time consuming and error prone. This my process for getting images broken up into sets in an automated way. All of the directories mentioned below are in my main Photos directory.

  • Step 1 - Transfer all of the photos off of the camera or other media and onto the destination drive. It’s essential to get them all onto the drive before starting to organize them so that you (and the script in step 3) don’t have to wait for the transfer process. My folder is called ‘1_incoming’. The ‘1_’ prefix is so that it’s always easy to find at the top of my Photos folder. It looks something like this after I get everything dumped in there.

Incoming folder screenshot

  • Step 2 - Create an empty directory that will be used as an intermediate directory during the process. Mine is called ‘1_in_transit’. I use this transit folder instead of just running the script on the incoming folder so that I can make sure that sets are getting broken up correctly. If I have any pictures mixed in that are just single shots or only a partial set I move those out of the way instead of into the transit folder. It’s a huge pain to have to try to undo several folders worth of sets because a stray image or two managed to sneak in.
  • Step 3 - Open a terminal window, cd into your ‘in transit’ folder and then run this HDR Directory Watcher Script. When you download the script make sure to remove the .txt extension (I had to add it to get WordPress to take it as an allowed upload) and run ‘chmod 755 hdr_dir_watcher.sh’ to make it executable. Then you might want to put in somewhere that’s on your PATH so it’s easy to execute.

To watch the directory you’re currently in:

hdr_dir_watcher.sh

To watch another directory

hdr_dir_watcher.sh /path/to/watch

  • Step 4 - Start dragging and dropping sets of 5 images at a time into the transit folder. You can do multiple sets of 5 images at a time too. The script will be watching this folder for files and when it finds them it will create a directory in the Photos directory named with todays date. Then inside that folder it will create a series of numbered folders (1, 2, 3, etc…) that correspond to each set of 5 images that go into the transit folder. So for todays date the directory would be 2007_04_01. After dragging some files into the transit directory Photos/2007_04_01/1 will have your first set of 5 images and Photos/2007_04_01/2 will have the next set of 5 and so on.

Photos broken up into set directories

Now you’ll be ready to start assembling some HDR images by hand or you’ll just have to wait for Part 2 of the series where I’ll detail how to automate the HDR assembly process.

Script Details

If you need to use the script with a different number of images per set you can change the SETSIZE variable. You could also change the date format if you want something different. Here’s the text of the script.

#!/bin/bash
DIR=.

if [ $# = 1 ]; then
  echo “watching custom dir: $1″
  DIR=$1
fi

SETSIZE=5
CHECK_INTERVAL=1.0
DATESTRING=$(date +%Y_%m_%d)

DIRNUM=1

cd $DIR
CURDIR=pwd
echo “watching $CURDIR”
echo “hit ctl-c to end”

while true
do
  sleep $CHECK_INTERVAL
  FILECOUNT=$(ls -1 | wc -l)
  let “FILEMOD=$FILECOUNT % $SETSIZE”
  if [ “$FILEMOD” -eq 0 ]; then

      if [ ! -d ../$DATESTRING ];then
	  mkdir ../$DATESTRING
      fi

      while [ $(ls -1 | wc -l) -ge $SETSIZE ]
      do
	DIREXISTS=true
	while $DIREXISTS
	  do
	  if [ -d ../$DATESTRING/$DIRNUM ]; then
	      ((DIRNUM++))
	  else
	      DIREXISTS=false
	  fi
	done
	mkdir ../$DATESTRING/$DIRNUM
	echo $(ls -1 | wc -l)
	echo $DIRNUM
	IMGNUM=0;
	for img in *
	do
	  if [ $IMGNUM -lt $SETSIZE ];then
	      mv $img ../$DATESTRING/$DIRNUM
	      echo “moved $img to ../$DATESTRING/$DIRNUM (ctl-c to end)”
	      ((IMGNUM++));
	  fi
	done
	((DIRNUM++));

      done
  fi

done

I’d love to have any feed back anyone might have on this or I’d just be interested to know if you’re using it. Stay tuned for parts 2 and 3 which will cover assembling each set of images into an HDR format image, and then tone mapping the HDR back into a normal (or Low Dynamic Range) image.

Leave a Reply