tactical vim

nerdy bits & pieces

Archive for November 2011

normalize phone JPG files based on EXIF data

leave a comment »

I recently decided to switch to Sprint (from T-Mobile, their network is awful here in San Antonio) and picked up the Motorola Photon 4G (great phone btw) which finally has a decent naming convention for mobile pics. For years I’ve been at the mercy of various HTC, Samsung and LG devices which randomly choose different conventions – in my mind, naming them by date/time is the best way to go and the Photon pleases me. Time to fix all the old (couple hundred) pics I like keeping on my phone to the Photon’s convention.

I found a script which introduced me to JHead so I built upon it – I quickly realized that all my EXIF date headers were whacked out too, all the various cameras stored them differently (using ‘:’ vs. ‘/’ vs. ‘-’ as field separators). This script fixes all that and resets the EXIF header date/time to jheads default.

MAKE A BACKUP OF YOUR PICS FIRST

#!/bin/bash
# normalizeJPEG.sh
#
# requires "jhead" (yum/apt-get install jhead)
# normalize JPG files based on their EXIF header - handy for loads of pics
# taken with different cameras (several phones, e.g.) that store the EXIF
# dates in different formats and name the files differently.
#
# Result: YYYY-MM-DD_HH-MM-SS_###.jpg (where ### is random 100-999)
# 
# This script also updates date/timestamp on disk, then reverse updates the
# EXIF header from that stamp to normalize your headers DATE/TIME field
#
# Original: http://neverfear.org/blog/view/148/Rename_all_jpeg_files_by_their_exposure_date_Bash
 
for file in *.jpg *.jpeg *.JPG *.JPEG; do
  if [ ! -e "$file" ]; then
    continue
  fi
  echo "Inspecting $file"
  JPGDT=`jhead "$file" | grep "Date/Time"`
  JPGFC=`jhead "$file" | grep "Date/Time" | wc -l`
  if [ $JPGFC -gt 1 ]; then
    echo "Found too many results for: $file"
    continue
  elif [ $JPGFC -eq 0 ]; then
    echo "No valid headers found."
    continue
  else
      BASE=`echo $JPGDT | awk '{ printf("%s %s",$3,$4) }' | sed -e 's/\([0-9][0-9]\).\([0-9][0-9]\).\([0-9][0-9]\) \([0-9][0-9]\).\([0-9][0-9]\).\([0-9][0-9]\)/\1-\2-\3 \4:\5:\6/'`
      BASE=`date -d "$BASE" +"%Y-%m-%d_%H-%M-%S"`
      RND=$[($RANDOM%899)+100]
      if [ ! -e "${BASE}_${RND}.jpg" ]; then
        echo "New file: ${BASE}_${RND}.jpg"
        mv "$file" "${BASE}_${RND}.jpg"
        jhead -ft "${BASE}_${RND}.jpg"
        jhead -dsft "${BASE}_${RND}.jpg"
      fi
  fi
done

Written by troyengel

2011-11-12 at 10:14

Follow

Get every new post delivered to your Inbox.