Extract the Exif DateTimeOriginal field and prepend it to the file name to facilitate image sorting

  • Posted on: 19 May 2014
  • By: Michael

Clients!? Clients are wonderful things. They pay the bills. But they definitely ask you for some odd things.

I have a client who needed 10s of thousands of images, from different sources, to be displayed by the date they were taken. As he didn't want to spend hours in his favorite image editor, checking the properties of each file one by one, he punted the whole thing to me with a: "Hey, take these images, and add them to the gallery and make sure they display by date."

As it's my business to make clients happy, my reply, "Sure. I'll have that for you in a few days."

Luckily, he created most of the files, so the Exif data was intact. That being so, I wrote a throw away script to sort the mess out.

Here's the script, and don't blame me if it breaks something :)

#!/bin/bash
#
#  Extracts the Exif:DateTimeOriginal field and prepends
#  it to the image file to facilitate sorting.
#
#  One off script, possibly has bugs, use at your own risk.
#
#  You will need imagemagick:
#    yum install imagemagick
#    apt-get install imagemagick
#    (whatever you use to install it)
#
# Script=addexiftime
# Author=Michael
# Email=staff_scripts at the commercial domain inet-design
# Website=inet-design.com
# License=GPL
# Script repository=
# # # #
 
#   Prettify things
INVT="\033[7m";NORM="\033[0m";BOLD="\033[1m";BLINK="\033[5m";BLACK_F="\033[30m";BLACK_B="\033[40m";RED_F="\033[31m";RED_B="\033[41m";GREEN_F="\033[32m";GREEN_B="\033[42m";YELLOW_F="\033[33m";YELLOW_B="\033[43m";BLUE_F="\033[34m";BLUE_B="\033[44m";MAGENTA_F="\033[35m";MAGENTA_B="\033[45m";CYAN_F="\033[36m";CYAN_B="\033[46m";WHITE_F="\033[37m";WHITE_B="\033[47m";
####
 
#  Who am I?
MyName=`basename "$0"`
 
#  Check usage
if [ "$1" != "" ] && [ ! -d "$1" ] ; then
  echo -e "$YELLOW_F""Usage               :""$NORM  $GREEN_F""$MyName [\"Path\"] [\"File Filter\"]""$NORM"
  echo "Default Path        :  Current Directory"
  echo "Default File Filter :  *.jpg"
  echo -e "$YELLOW_F""Example             :""$NORM  $GREEN_F""$MyName \"/home/user/some directory\" \"*.jpeg\"""$NORM"
  exit
fi
 
#  Say Hello
StartTime=`date +%s`
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo -e "$RED_F$CurrDateTime:$GREEN_F$BOLD Starting$NORM \"$MyName\" It can take awhile, need coffee?"
 
#  Do the work
if [ "$1" != "" ] ; then
  cd "$1"
fi
 
ResultsDir="`pwd`/results"
 
FileExt="*.jpg"
if [ "$2" != "" ] ; then
  FileExt="$2"
fi
 
FileList=`find ./ -maxdepth 1 -type f -name "$FileExt"`
FilesToProcess=`echo "$FileList" | wc -l`
 
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Adding EXIF timestamps and copying to: $ResultsDir"
echo -e "$YELLOW_F$D00$NORM - $GREEN_F$MESG$NORM"
MESG="Files to Process : $FilesToProcess"
echo -e "$YELLOW_F$D00$NORM - $GREEN_F$MESG$NORM"
 
FilesProcessed="0"
mkdir -p results
 
while IFS= read -r filename
do
  DateString=""
  NameofFile=`basename "$filename"`
  DateString=`identify -verbose "$NameofFile" | grep exif:DateTimeOriginal | awk '{ print $2" "$3 }' | sed -e s/":"/"."/g -e s/" "/"."/g`
  if [ "$DateString" = "" ]; then
    DateString="1900.00.00.00.00.00"
  fi
  cp "$filename" "results/$DateString-$NameofFile"
  FilesProcessed=$((FilesProcessed + 1))
  D00=`date +"%y-%m-%d %H:%M:%S"`
  MESG="Files Processed  : $FilesProcessed"
  echo -en "$YELLOW_F$D00$NORM - $GREEN_F$MESG$NORM"
  echo -en "\r"
done <<< "$FileList"
 
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Files Processed  : $FilesProcessed"
echo -e "$YELLOW_F$D00$NORM - $GREEN_F$MESG$NORM"
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Files in $ResultsDir : `ls -1 "$ResultsDir" | wc -l`"
echo -e "$YELLOW_F$D00$NORM - $GREEN_F$MESG$NORM"
 
#  Say Goodbye
END_TIME=`date +%s`
ELAPSED=`expr $END_TIME - $StartTime`
remainder="$(expr $ELAPSED % 3600)"
hours="$(expr $(expr $ELAPSED - $remainder) / 3600)"
seconds="$(expr $remainder % 60)"
minutes="$(expr $(expr $remainder - $seconds) / 60)"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo -e "$RED_F$CurrDateTime:$GREEN_F$BOLD Finished$NORM \"$MyName\"  Elapsed time: `printf %02d $hours`:`printf %02d $minutes`:`printf %02d $seconds`"
Tags: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
You know what to do... An image you see? there is one finger less than two hands of items to find. (We sincerely apologize for using image CAPTCHA, hint go low, and bots suck...)
Image CAPTCHA
Enter the characters shown in the image.