Wednesday, November 24, 2010

Easily moving pictures from camera to hard drive

Getting files from the camera to hard drive is sometimes accompanied by confusion and consternation when custom actions are needed. Given powerful tools and a bit of facility with scripting, retrieving the files can be as simple as

movePics.sh <fromDir> <toDir>

A special love note for my better half. For the old camera disk use

/DATA01/bin/movePics.sh /media/disk/DCIM/101MSDCF /DATA01/Pictures 

For the new camera disk 1 use

/DATA01/bin/movePics.sh /media/1A48-D026/DCIM/100D7000 /DATA01/Pictures

Love, your system administrator.

The heart of this solution is the perl exiftool (http://owl.phy.queensu.ca/~phil/exiftool/) that can interrogate, modify, and otherwise act on tags in image files. This article describes an application of the renaming functionality (http://owl.phy.queensu.ca/~phil/exiftool/filename.html). Specifically, when moving the files from the camera media (e.g. /media/1A48-D026/DCIM/100D7000) to the root of an images directory (e.g. /DATA01/Pictures) the requirements are:

  1. Separate pictures into directories by date
  2. Format of the directories are year/month/year-month-day where the date is given in digits only
  3. Create directories if they do not already exist
  4. Ensure that the extensions are lower case
    • The need for this was driven by the use of some web applications that do not display files with uppercase extensions
  5. The date information shall come from the original date and time in the image header

All these requirements are met in a single invocation

exiftool '-FileName<DateTimeOriginal' \
 -d /DATA01/Pictures/%Y/%m/%Y-%m-%d/%%f.%%le \
 /media/1A48-D026/DCIM/100D7000

Two potentially tricky bits are the double percent (%%) for the file and extension formats (%%f and %%e respectively), and the lowercase modifier for the extension (%%le). The need for quotes around the first argument is because the exiftool syntax < would otherwise be interpreted by the shell instead of being sent to the script.

While the invocation is not terribly difficult, there is enough typing to make it worthwhile for a simple script (e.g. movePics.sh). A minimal implementation that takes two arguments — a from and to directory — is written here in bash.

#!/bin/sh
srcdir=$1
tgtdir=$2
exiftool '-FileName<DateTimeOriginal' \
 -d $tgtdir/%Y/%m/%Y-%m-%d/%%f.%%le $srcdir

No comments:

Post a Comment