How To: Automate HDR on Linux - Part 1.1

OK, it’s been a loooong time since I’ve done much with the blog. I actually got an email the other day from someone asking if I’d ever gotten around to finishing the series. I was inspired to know that someone had even found it, so I figured I’d get back to work. Thanks Iain! Anyway, over the last year or so the script that I presented in Part 1 has changed a bit, so I thought I should post an update.

The script works just about the same as the first one. It now takes command line arguments. It has 3.

1. Give it a set size

Pass in a set size to let the script know how many photos are in your bracketed set.

Example:

hdr_dir_watcher.sh 3

2. Give it a size and a destination directory

Pass in a destination directory to customize the location of the numbered directories. By default the directories will be located in the directory that the script is run in.

Example

hdr_dir_watcher.sh 3 /some/dir

3. Give it a set size, a destination directory, and a directory to watch.

Pass in a custom directory for the script to watch. By default the script with watch the directory that it is run in.

Example

hdr_dir_watcher.sh 3 /some/dir /some/other/dir

Download the HDR Directory Watcher Script

And here’s the actual code.

#!/bin/bash
################################
# Written by Jeremy Green
# http://www.rhythmandcode.com/
################################
function makeFullName(){
    FULLNUM=''
    if [ $1 -lt 10 ]; then
	FULLNUM='000'${1}
    elif [ $1 -lt 100 ]; then
	FULLNUM='00'${1}
    elif [ $1 -lt 1000 ]; then
	FULLNUM='0'${1}
    else
	FULLNUM=${1}
    fi 

}

DIR=.
DEST='date'

if [ $# -eq 0 ]; then
    echo "Usage : hdr_dir_watcher.sh setsize [destination_dir watch_dir]"
    exit 0;
fi

SETSIZE=3
if [ $# -ge 1 ]; then
    echo "set size: $1"
    SETSIZE=$1
fi

DIR=.
DEST=.

if [ $# -ge 2 ]; then
  echo "moving to custom dir: $2"
  DEST=$2
fi

if [ $# -ge 3 ]; then
  echo "watching custom dir: $3"
  DIR=$3
fi

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

if [ $DEST = 'date' ]; then
    DEST=../$DATESTRING
fi

DIRNUM=1

cd $DIR
#CURDIR=pwd
#echo "watching $CURDIR"
echo "hit ctl-c to end"

while true
do
  sleep $CHECK_INTERVAL
  FILECOUNT=$( find ./ -maxdepth 1 -type f  | wc -l )
  let "FILEMOD=$FILECOUNT % $SETSIZE"
  if [ "$FILEMOD" -eq 0 ]; then

      if [ ! -d $DEST ];then
	  mkdir $DEST
      fi

      while [ $( find ./ -maxdepth 1 -type f | wc -l ) -ge $SETSIZE ]
      do
	DIREXISTS=true
	while $DIREXISTS
	do
	    makeFullName $DIRNUM
	    if [ -d $DEST/$FULLNUM ]; then
		((DIRNUM++))
	    else
		DIREXISTS=false
	    fi
	done

	mkdir $DEST/$FULLNUM
	#echo $(ls -1 *.* | wc -l)
	echo $FULLNUM
	IMGNUM=0;
	for img in $( find ./ -maxdepth 1 -type f | sort -n )
	do
	  if [ $IMGNUM -lt $SETSIZE ];then
	      mv $img $DEST/$FULLNUM
	      echo "moved $img to $DEST/$FULLNUM (ctl-c to end)"
	      ((IMGNUM++));
	  fi
	done
	((DIRNUM++));

      done
  fi

done

Leave a Reply