Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for changing photo creation timestamp (in EXIF metadata and filesystem) in all jpeg's in current directory.
#!/usr/bin/ruby
# encoding: utf-8
#
# Script for changing photo creation timestamp (in EXIF metadata and filesystem)
# If you're using rvm, please run script as `ruby photo-touch.rb`
# Tested in ruby-1.9.3-p125 and ree-1.8.7-2012.02
#
# Install ExifTool: http://www.sno.phy.queensu.ca/~phil/exiftool/install.html
#
# You need next gems for script to work (use gem install gem_name)
# * active_support (~> 3.0)
# * mini_exiftool (note: you need the ExifTool library!)
#
# 2012, Novikov «Envek» Andrey. Use it freely under the MIT license.
#
require 'rubygems'
require 'active_support/all' # A lot of useful stuff here (e.g. 2.weeks.ago)
require 'find' # For Find.find (walk a directory)
require 'fileutils' # For FileUtils.touch (change file timestamps)
require 'mini_exiftool' # Exif metadata editing
# http://stackoverflow.com/questions/4894198/how-to-generate-a-random-date-in-ruby
def time_rand(from = 0.0, to = Time.now)
Time.at(from + rand * (to.to_f - from.to_f))
end
### CONFIG SECTION ###
offset = 1.day + 4.hours - 20.minutes # Shift all timestamps back by offset
#new_time = time_rand((1.day+12.hours).ago, (1.day+6.hours).ago) # Set a random timestamp in range
#date = (1.day+8.hours+10.minutes).ago # To a some defined datetime
#offset = (1.hours / Find.find('./').count).seconds # For giving all photos sequentally growing timestamps
# Find all jpegs in currrent directory, change timestamps to required
Find.find('./') do |f|
if f.match(/\.jpg|jpeg|jfif\Z/i)
photo = MiniExiftool.new(f)
date = photo.date_time_original - offset # Calculate it here as you wish
puts "#{f}\t- #{date}" # For debug purposes
photo.date_time_original = date # Change EXIF photo creation timestamp
photo.save
FileUtils.touch f, :mtime => date # Change filesystem creation timestamp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment