Me & 3M

My Mundane Musings

Renaming Photos with DateTime key

After many a trips, I will have photos from all the shutterbugs – covering A-Z of the trip.

Having photos from various sources, all pertaining to the same context, event or trip, how do I view the photos with out loosing the flow?

There are various software, using which I can store, manage these photos. Software album makers like Google’s Picasa and ACDSee are excellent photo managers, but how do we tackle this flow problem?

Time is the key!! All digital cameras store EXIF information along with the JPEG header. I wrote a small C code to extract the DataTime EXIF tag from my photos using the libexif library.

Using another script, I can now rename/copy images in proper order. It is assumed that there is no time difference/lag between the camera sources.

What ever you do, it will be a good idea to sync Date/Time field of all cameras before shooting or starting the trip. Alternatively, time difference +/- can be adjusted by adding 2 more lines to the script to incorporate this allowance.

This code uses libexif Exif tag parsing library.

getexifdatetime.c

#include "stdio.h"

#include "libexif/exif-utils.h"
#include "libexif/exif-data.h"

// cc -lexif getexifdatetime.c -o getexifdatetime

int
main (int argc, char **argv)
{
  ExifData *ed;
  ExifTag et;
  ExifEntry *ee;
  char val[30];
  unsigned int i;

  ed = exif_data_new_from_file (argv[1]);
  et = EXIF_TAG_DATE_TIME;
  for (i = 0; i ifd[i], et)
    {
      if (ee)
	{
	  printf ("%s", exif_entry_get_value (ee, val, 30));
	}
    }

  exif_entry_free (ee);
  exif_data_free (ed);
}

rename2jpg-datekey.bash

#!/bin/bash
#####################################################################
# After getting back from an outing, collection of photos from different sources
# need to be sorted according to time stamp for better flow
# Here I use ImageMagic's identify command to get time stamp and rename/copy
# the source file to a destination with following format string
# Input: List of jpg directories,
#	 directory name can be user/location name for better meaning
# Output: $PREFIX-$TIME_STAMP.$SUFFIX.jpg
#	 All files goes to $PWD/renamed-with-date-stamp
#	 PREFIX can be like Image or Photo or Location name etc..
#	 SUFFIX can be like Author or Location etc..
#####################################################################
# Ajith/021006/First release
#####################################################################
# TODO
# GUI for input/format selection etc..
#####################################################################

DEST=$PWD/renamed-with-date-stamp

if [ $# -lt 2 ]
then
	echo "No input directories given, quitting"
	echo "Minimum args: PREFIX and Input DIRECTORY[..]"
	exit
else
	# PREFIX=Image
	# PREFIX=Photo
	PREFIX=$1
	echo "Using $PREFIX as image prefix: $PREFIX-\$TIME-STAMP.\$SUFFIX.jpg"
	shift
	echo "Creating files in $DEST"
	mkdir -p "$DEST"
fi

for d in $*
do
	if [ ! -d "$d" ]
	then
		echo "$d is not a directory"
		continue
	fi
	pushd "$d"
	img_suffix=$(basename $d)
	for f in *.jpg
	do
		if [ "$f" = "*.jpg" ]
		then
			echo "No more files here"
			break
		fi
		TSF="${PREFIX} $(getexifdatetime "$f";) ${img_suffix}.jpg"
		if [ "$TSF" = "${PREFIX} ${img_suffix}.jpg" ]
		then
			echo "$f doesn't seem to have a time stamp"
			continue
		fi
		# Change : to _ for DOS compatibility
		TSF=$(echo $TSF | tr [:] [_] ;) 
		echo -n cp "$f" "$TSF .."
		if [ -f "$DEST/$TSF" ]
		then
			echo " failed, image already exists."
		else
			cp "$f" "$DEST/$TSF"
			echo " done"
		fi
	done
	popd
done

This script can be easly integrated with Nautilus file manager for use from Gnome desktop.

powered by performancing firefox

October 6, 2006 - Posted by | Tech Babble, Tips and Tricks

2 Comments »

  1. It was fun writing this piece of code, but a google search for file renamer will give you more mature solutions :)

    Recently I came across a small utility program renamer, it got pascal scripting in the backend and very powerful filter capabilities, all in a very intuitive & functional GUI. More over it worked without any hue & cry in Linux using Wine

    Comment by ajithc | October 7, 2006 | Reply

  2. Check out jhead: http://www.sentex.net/~mwandel/jhead/

    Comment by Vynce | December 29, 2006 | Reply


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.