Skip to content

Instantly share code, notes, and snippets.

@alext
Created April 12, 2016 21:49
Show Gist options
  • Save alext/d75c230c7de61ffb81000362d4b94290 to your computer and use it in GitHub Desktop.
Save alext/d75c230c7de61ffb81000362d4b94290 to your computer and use it in GitHub Desktop.
Script to adjust the EXIF date of an image file by a given offset in days.
#!/usr/bin/env ruby
require 'open3'
require 'time'
EXIF_DATE_FORMAT='%Y:%m:%d %H:%M:%S'
offset = ARGV[0].to_i
file_name = ARGV[1]
raise "Can't find file #{file_name}" unless File.exist?(file_name)
file_date, status = Open3.capture2('exiftool', '-p', '$DateTimeOriginal', file_name)
raise "Failed to read date" unless status.success?
file_date.strip!
date = DateTime.strptime(file_date, EXIF_DATE_FORMAT)
new_date = (date + offset).strftime(EXIF_DATE_FORMAT)
puts "#{file_name}: #{file_date} => #{new_date}"
output, status = Open3.capture2e('exiftool', "-DateTimeOriginal=#{new_date}", "-CreateDate=#{new_date}", "-ModifyDate=#{new_date}", file_name)
raise "Failed to set dates: #{output}" unless status.success?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment