public
Last active

Python - Fix Photo Exif Metadata

  • Download Gist
img_exif_date_fixer.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!/usr/bin/env python
#
# gexiv2 image Exif date fixer.
# Corey Goldberg, 2014
 
 
"""Recursively scan a directory tree, fixing dates
on all jpg/png image files.
 
Each file's Exif metadata and atime/mtime are all
set to the file's ctime.
 
Modifications are done in-place.
 
Requires: gexiv2
"""
 
 
import os
import time
 
# GObject-based wrapper around the Exiv2 library.
# sudo apt-get install gir1.2-gexiv2-0.4
from gi.repository import GExiv2
 
 
def fix_image_dates(img_path):
t = os.path.getctime(img_path)
ctime = time.strftime('%Y:%m:%d %H:%M:%S', time.localtime(t))
exif = GExiv2.Metadata(img_path)
exif['Exif.Image.DateTime'] = ctime
exif['Exif.Photo.DateTimeDigitized'] = ctime
exif['Exif.Photo.DateTimeOriginal'] = ctime
exif.save_file()
os.utime(img_path, (t, t))
 
 
if __name__ == '__main__':
dir = '.'
for root, dirs, file_names in os.walk(dir):
for file_name in file_names:
if file_name.lower().endswith(('jpg', 'png')):
img_path = os.path.join(root, file_name)
fix_image_dates(img_path)

Thanks!

I'll go to add some metada and tags. It looks very usefull.

Maybe you should explicitly mention, that this will overwrite the times already present in any existing EXIF metadata.

(I wrote this comment here, because your blog only allows comments with a Google+ account. Please rectify that.)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.