Idaho Fair

Previously I talked about how to add date time stamp to your photos using completely free software. It was written mostly for Microsoft Windows users. The basic method should work on Mac, Linux, or other Unix flavors but there are obviously quite many differences between Windows batch file and shell scripts.

So I found sometime to create a shell script version to do just that for people who don’t use Windows. There is nothing fancy but it should work for typical users.

To get it started, follow the simple steps.

1. Install ImageMagick

How to do it is up to you. Personally I installed the MacPorts first, then installed ImageMagick by running a simple command “sudo port install ImageMagick” in a terminal on a Mac running Leopard. You can certainly try the pre-compiled binaries if you’d like.

2. Get the font

To make it look like the old 35mm point-and-shoot film camera’s time stamp, you can use a LCD font. Simply downloaded it and unpacked it somewhere. You don’t have to install the fonts. The script can work by a direct link to the font file location.

3. Get the code

The following is the complete shell script. Tested on a Mac running bash.

#!/bin/sh
# Change the font variable to point to your font location
font="/Users/max/Library/Fonts/digital-7 (mono).ttf"

if [ $# -eq 0 ]
   then
      cat << _EOF_

USAGE: $0 file1 file2 ..., or
       $0 *.jpg, or
       $0 dir/*.jpg
       ...

_EOF_
      exit
fi

while [ "$1" != "" ]; do
        # Skip directories
        if [ -d "$1" ]; then
                shift
                continue
        fi
        # Skip already converted files (may get overwritten)
        if [[ $1 == *_DT* ]]
        then
                echo "------  Skipping: $1"
                shift
                continue
        fi

        # Work out a new file name by adding "_DT" before file extension
        file=$1
        echo "######  Working on file: $file"
        filename=${file%.*}
        extension=${file##*.}
        output=${filename}_DT.${extension}

        # Get the file dimension
        dim=$(identify -format "%w %h" "$file")
        width=${dim%% *}
        height=${dim#* }

        # Decide the font size automatically
        if [ $width -ge $height ]
        then
                pointsize=$(($width/30))
        else
                pointsize=$(($height/30))
        fi

        echo "        Width: $width, Height: $height. Using pointsize: $pointsize"

        # The real deal here
        convert "$file" -gravity SouthEast -font "$font" -pointsize $pointsize -fill white -annotate +$pointsize+$pointsize "%[exif:DateTimeOriginal]" "$output"

        shift
done

exit 0

4. Do the work

Copy the code into a text editor, save it as “dtstamp.sh” or anything you like. Please note you need to change the 3rd line in the code to point to the font file location.

Run the following command to make it executable

chmod +x dtstamp.sh

To use it, simply run it like this

./dtstamp.sh directory/*.jpg

Or,

./dtstamp.sh file1.jpg file2.jpg...

After it runs, the script will produce files with “_DT” inserted between the original file name and file extension. The original file is unmodified.

Posted in: Tips and Techniques on August 27th, 2009. Trackback URI
Keywords: , ,

Related Posts

Comments

  • http://linux.aldeby.org aldeby

    Dear Max,
    Thank you so much for this script!
    I was really looking for some open source way to add the time stamp to a bunch of pictures and didn’t know there was such an easy way using free and open source software!

    Just a note:

    To those getting the following terminal output:

    convert: unable to read font `/Users/max/Library/Fonts/digital-7 (mono).ttf’ @ warning/annotate.c/RenderType/807.

    remember you have to change the font path as commented in the script!

    Maybe Max you could suggest users to download the font file in the .fonts folder (the user space default font folder) and instead of your path put in your script this line

    font=”$HOME/.fonts/digital-7 (mono).ttf”

    where the $HOME variable being replaced with the user’s username.

    Again many thanks! Wish you a great day Max!

  • http://linux.aldeby.org aldeby

    I mean, $HOME would be automatically replaced with the current user’s username by any linux system every time the script will be run.

  • gionnico

    If you want to print a custom timestamp, here’s how you do that.
    Replace the final part of the while loop with:

    # Custom timestamp string
    IFS=$IFS: read -r year month day hour minute second <<< $(identify -format "%[EXIF:DateTimeOriginal]" "$file");
    DTString="$month/$day/$year $hour:$minute:$second"

    # The real deal here
    convert "$file" -gravity SouthEast -font "$font" -pointsize $pointsize -fill white -annotate +$pointsize+$pointsize "$DTString" "$output"

  • http://www.osugisakae.com Osugi Sakae

    Thanks for the great script. I have been wanting to add timestamps to my photos for a while now, and knew IM must be able to do this sort of thing. Never could figure it out, though.

    I have a quick addition to your script. Since white will often disappear on top of a photo, I added

    -undercolor ‘#00000080′

    to the convert command, to add a transparent gray background behind the timestamp. The command looks like this:

    convert “$file” -gravity SouthEast -font “$font” -pointsize $pointsize -undercolor ‘#00000080′ -fill white -annotate +$pointsize+$pointsize ” %[exif:DateTimeOriginal] ” “$output”

    Because of the way the background fits the time and date, I also added a space before and after the timestamp (” %[exif:DateTimeOriginal] “). Makes it look a bit nicer.

    Another great addition to your script would be to detect image rotation and either skip that photo, or automagically rotate the timestamp to match. If I come up with something, I will post it here.

    • http://dptnt.com/ picmax

      Great tips! Thank you very much for your contribution.

    • Jim Navarro

      I haven’t done Ba$h for long time. Certainly the original script works and the custom timestamp strings with mine, too. Oh my Fedora 12 doesn’t know undercolor ‘#00000080? so the “ were replaced with quotes and it works! Somehow the undercolor doesn’t fill the whole background of the timestamp.

  • JohnW

    Thanks for this – it was very useful to me as it showed me the syntax for the convert command. (I didn’t even know it could do this.) I have an IP security camera which snaps away when there is any activity and I use a script to convert the still images into a video. I create the videos at 20fps for easier reviewing (unfortunately I get a lot of false alarms). Now, I have added the addition of a time stamp to each frame before the video is created so if there is something I need to look more closely at I can easily locate the original stills (whose file base names are the date and time).