Last active
July 31, 2018 13:17
-
-
Save rmmh/a1e79dfdd5c91195878a762b8ffc0093 to your computer and use it in GitHub Desktop.
Simple script to adjust a series of photos to add missing exif date information in a series of photos.
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
#!/usr/bin/env python | |
# Problem: Our maternity photos were mixed digital and film, | |
# and the exif dates/times for the film photos were wrong, preventing | |
# "sort by date" working in Google Photos. | |
# | |
# Solution: the filenames are in the right order, simply interpolate | |
# over the gaps to inject "correct" times. | |
import glob | |
import subprocess | |
import datetime | |
def get_timestamp(path): | |
result = subprocess.check_output(['exiv2', path]) | |
for line in result.splitlines(): | |
if line.startswith('Image timestamp'): | |
return line.split(':', 1)[1].strip() | |
def get_timestamps(): | |
jpgs = sorted(glob.glob('*.jpg')) | |
times = [(j, get_timestamp(j)) for j in jpgs] | |
return times | |
def find_gaps(times): | |
start = '' | |
end = '' | |
in_gap = False | |
gaps = [] | |
for j, t in times: | |
if t: | |
if in_gap: | |
in_gap = False | |
gaps[-1][1] = t | |
else: | |
start = t | |
else: | |
if in_gap: | |
gaps[-1][2].append(j) | |
else: | |
in_gap = True | |
gaps.append([start, '', [j]]) | |
assert not in_gap | |
return gaps | |
def lerp_gaps(gaps): | |
TFMT = '%Y:%m:%d %H:%M:%S' | |
new_times = [] | |
for start, end, jpgs in gaps: | |
print start, end, jpgs | |
s = datetime.datetime.strptime(start, TFMT) | |
e = datetime.datetime.strptime(end, TFMT) | |
for j in jpgs: | |
s += datetime.timedelta(seconds=1) | |
assert s < e, (s, e) | |
t = s.strftime(TFMT) | |
new_times.append((j, t)) | |
return new_times | |
def set_times(assign): | |
for j, t in assign: | |
print j, t | |
subprocess.check_output(['exiv2', '-Mset Exif.Image.DateTime ' + t, j]) | |
def main(): | |
times = get_timestamps() | |
for j, t in times: | |
print j, t | |
gaps = find_gaps(times) | |
assign = lerp_gaps(gaps) | |
set_times(assign) | |
print 'done!' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment