exif date shifting for photos

I recently pulled about 6 months of photos from my camera. To my aghast I realized that I had mistakenly set the clock a year early so all of the photos had the correct date on them except for the year which was 2006 instead of 2007. This was really annoying in Picasa which uses the date of the photo in the exif header to order photo albums.

In order to fix this I need to update the EXIF file for each JPEG. What I really needed was a date shift since most of the dates I was using were fine. I did some digging around and found ExifTool by Phil Harvey that will let me modify an EXIF file, in particular ExifTool will perform a date shift on a group of photos - probably for morons like me who can’t properly set their camera date or accidentally check in a hard coded test URL.

ANYWAY I had quite a few photos to process and some of the photos (first half of 2007) that were correct. So I wrote a script that did the following:

1) Check the EXIF create_date for 2006.
2) If 2006 shift the date forward one year
3) confirm and delete the backup “original”

Here is the subsequent ruby script. It isn’t very pretty as this was a one off operation.

shift_year_forward_to_2007_if_2006.rb

#!/usr/bin/env ruby
require 'date'

def create_year(photo)
  command = "exiftool -CreateDate #{photo}"
  read_process = IO.popen(command)
  output = read_process.readline
  read_process.close
  Date.parse(output.split[3].gsub!(":","-")).year
end

photos = Dir[File.join('**/*')].select do |path| path.upcase.include?("JPG") end
photos.each do |photo|
    if create_year(photo).eql?(2006)
      puts "Attempting to Shift #{photo}..."
      modify_process = IO.popen("exiftool \"-AllDates+=1:0:0 0\" #{photo}")
      modify_process.close
      File.delete("#{photo}_original")
      if create_year(photo).eql?(2007)
        puts "Verified!"
      else
        puts "Error on #{photo}"
      end
    else
      puts "Ignoring #{photo}"
    end
  end

One Response to “exif date shifting for photos”

  1. Phil Harvey Says:

    Interesting, but this could easily be done without a script with the following command:

    exiftol -ext JPG -if ‘$createdate =~ /^2006/’ -AllDates+=”1:0:0 0″ -overwrite_original DIR

    (if used in Windows, double quotes must be used instead of single quotes in the -if argument above)

Leave a Reply