Last active
January 9, 2017 01:21
-
-
Save peagha/709050cd12d9e4254d0ea0bc887bef7d to your computer and use it in GitHub Desktop.
Fixes my photos date / time by applying the difference between the registered and the correct date. Requires `mini_exiftool` Gem and "ExifTool by Phil Harvey"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mini_exiftool' | |
def correct_date_time(wrong_date_time, offset) | |
c = wrong_date_time + offset | |
# Removes timezone | |
Time.new(c.year, c.month, c.day, c.hour, c.min, c.sec) | |
end | |
def apply_offset_to_photo(path, offset) | |
photo = MiniExiftool.new(path) | |
date_time = correct_date_time(photo.date_time_original, offset) | |
update_date_time(photo, date_time) | |
photo.save! | |
end | |
# Manually selected every field in MiniExiftool.new(photo_path).to_hash that | |
# looked like a relevant date_time field. | |
# Those fields may be different depending on the camera used. | |
def update_date_time(photo, date_time) | |
photo.file_modify_date = date_time | |
photo.modify_date = date_time | |
photo.date_time_original = date_time | |
photo.create_date = date_time | |
# sony specific | |
photo.sony_date_time = date_time | |
end | |
def time_offset(photo_path, correct_date_time) | |
photo = MiniExiftool.new(photo_path) | |
correct_date_time - photo.date_time_original | |
end | |
# Here I pick a photo for witch I know the correct date / time. | |
# Any photo where a clock is visible is useful. | |
# Based on that I calculate the difference between the correct date / time and | |
# the registered date / time. | |
# Using that difference I can fix every photo's timestamp. | |
# | |
# TODO: mini_exiftool initializes Time objects using the machine's local | |
# timezone. Since EXIF tags don't store timezone, it's necessary to consider | |
# how timezones affect the calculations. | |
offset = time_offset('DSC05366.JPG', Time.new(2016, 12, 24, 12, 29, 0)) | |
Dir['./*.JPG'].each do |photo_path| | |
apply_offset_to_photo(photo_path, offset) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment