Over the years I’ve collected a lot of pictures, coming close to 20000. Most of these pictures have the exif
metadata embedded in the JPEG files. Alas, I was careless with some of the photographs, and when copying over from filesystem to filesystem, creating backups etc., the timestamps got overwritten. So now I had loads of files that had a timestamp of 22nd January 2010 for example. They were most definitely not taken at that date, but rather they were copied then and no preserve timestamp flag was enabled at the time of the cp
issued. I googled for a quick solution to my problems, but I could not find anything that would be simple to use, and not clogged up with bullshit. So, I cracked my knuckles and delved into the world of Python:
#!/usr/bin/python2
# You need the exifread module installed
import exifread, time, datetime, os, sys
def collect_data(file):
tags = exifread.process_file(open(file), 'rb')
for tag in tags.keys():
if tag in ('EXIF DateTimeOriginal'):
return "%s" % (tags[tag])
for file in sys.argv[1:]:
try:
phototaken = int(datetime.datetime.strptime(collect_data(file), '%Y:%m:%d %H:%M:%S').strftime("%s"))
os.utime(file,(phototaken,phototaken))
except Exception, e:
sys.stderr.write(str(e))
sys.stderr.write('Failed on ' + str(file) + '\n')
Basically it takes each file, reads the exif metadata for photo taken and invokes the os.utime()
function to set the timestamp to that date. You’ll need the exifread module for Python, this is the simplest one I could find that can do what I needed it to do. I hope someone will find this script useful. You can start it simply with $ exify *.JPG
. You can download it here.