The chocolate ration is being increased to 25 grammes per week.

Renaming photos with ExifTool

So, years ago I wrote a Lisp program to rename my photos to follow a specific convention (YYYYMMDD-HHMM-SSXX-CAM.EXT).  I use this because I can see at a glance what camera was used and when the photo was taken.  At this point I’m just too used to this to change.  The problem is that my program doesn’t deal with raw files, since they don’t have EXIF info but a camera-specific format.  ExifTool to the rescue.  It’s a Perl program and an amazing one at that.  Rather than fix my Lisp program to handle Canon raw files (the only ones I care about), I decided to switch to ExifTool.  It is really an amazing program.  Did I say that already?

The tricky bit was going to be getting the camera name part as an abbreviation.  The full name, Canon 5D Mark III, is readily available for use in file names, but I want something much shorter: 5D3.

The next issue was the “XX” part of the file name.  This was represented as a “sequence number”.  Say I take two photos in the same second of time (very  possible with my 5D, since I can take up to 7 photos a second).  I need that sequence number to distinguish like-named photos, otherwise they couldn’t coexist in the filesystem on my computer.  Canon has a “File Number” tag, which is something like “<folder>-<sequence>”.  <folder> is usually “100” and <sequence> is monotonically increasing for a given camera.  It’s basically a shutter count.  The problem is that non-Canon cameras don’t have File Number.  My Panasonic DMC-TS3 has a Sequence Number tag, however, which seems the same as I was using in my Lisp program.

I also want it to handle video files, which don’t share the same format with camera files, but ExifTool does handle this.

To put all this together, I need additions to ~/.ExifTool_config that create new tags, MyFileNumber and MyModel, that have the values I want, then it’s just a matter of calling ExifTool to do the renaming.

First, the ~/.ExifTool_config addition:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyModel => { # Abbreviate the model number for inclusion into filename
            Desire => {
	        0 => 'Model',
		# always exists, AFAIK:
	        1 => 'Make',
            ValueConv => q{
	    	my $name;
		$name = defined $val[0] ? "$val[0]" : "$val[1]";

		# H.264 videos have numeric Make
		# Numeric values are from:
                #   http://owl.phy.queensu.ca/~phil/exiftool/TagNames/H264.html
		if ($name =~ /^\d+$/) {
		    return "Pana-H264"  if $name ==  0x103;
		    return "Sony-H264"  if $name ==  0x108;
		    return "Canon-H264" if $name == 0x1011;
		    return "UNKNOWN-H264-VALUE:$name";

	        # Phones:
	        $name =~ s{Galaxy Nexus}{GN};

	    	# Canon:  ORDER IS IMPORTANT!
	        $name =~ s{Canon EOS 5D Mark III}{5D3};
	        $name =~ s{Canon EOS 5D Mark II}{5D2};
	        $name =~ s{Canon EOS 7D}{7D};

		# Panasonic, remove DMC- prefix, leave rest.
		$name =~ s{DMC-}{};

		return "$name";

        MyFileNumber => {
	    Desire => {
	    	# Canon.  Example: "100-3504".
	        0 => 'FileNumber',
		# Panasonic.  Usually 0.
		1 => 'SequenceNumber',
		# dummy value that must exist and is ignored.
		2 => 'Make',
	    ValueConv => q{
	    	if (defined $val[0]) {
		    # probably Canon, look for "100-" and remove it
		    my $fn = $val[0];
		    $fn =~ s{^100-}{};
		    return "-$fn";
		return "$val[1]" if defined($val[1]);
		return 0;
	    PrintConv => 'sprintf("%02s",$val)',

1;  #end

Then, I can call ExifTool to rename from my CF card like this:

exiftool -directory=d:/pictures/_TMP/_RAW/ -r -progress -v1 \
    -ext '*' --ext avi \
    --ext ctg --ext bdm --ext tid --ext tdt --ext mpl --ext cpi \
    -d %Y%m%d-%H%M-%S \
    '-filename<${DateTimeOriginal}${MyFileNumber}-${MyModel}.%e' \

This English version of that is: output to d:/pictures/_TMP/_RAW/ after recursively finding all files except ones that have certain extensions (avi, ctg, bdm, tid, tdt, mpl, cpi), using my date format specified, using a source directory G:/ (where my CF card is mounted in Windows), giving a progress as ExifTool does the work.

Again, I love ExifTool!


One response to “Renaming photos with ExifTool

  1. Jason Boxman March 8, 2013 at 5:55 am

    Thank you for this! It appears my Nikon D3100 also has a FileNumber field, but it is simply a zero padded four digit sequence. Definitely going to adopt the abbreviated model naming scheme.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: