Created
November 24, 2022 18:40
-
-
Save jeffbryner/4ccdb72e9d4e24571cb45713e90d4ee3 to your computer and use it in GitHub Desktop.
Add missing exif date from filename
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
# small script to add exif date tags to photos that are missing them | |
# assumes the date is in the filename in the format: <something>-DATE-anythingelse.<jpg|png|etc> | |
# and that the date can be parsed by dateutil.parser | |
# dependencies | |
# pip install exif, python-dateutil | |
import exif | |
import glob | |
from exif import Image | |
from dateutil.parser import parse | |
from exif import Image, DATETIME_STR_FORMAT | |
dry_run=False # False if you'd like to make changes and add exif date tags | |
show_no_ops = False # True will print files that are ok and have exif date tags | |
file_list = glob.glob("/Users/jeffbryner/Documents/pics/2014/*") | |
for target in file_list: | |
with open(target, 'rb') as image_file: | |
my_image = Image(image_file) | |
if my_image.has_exif and my_image.get("datetime_digitized"): | |
if show_no_ops: | |
print(f"{target} is ok") | |
else: | |
print(f"{target} missing exif datetime_digitized") | |
if not dry_run: | |
try: | |
objDate=parse(target.split("-")[1],fuzzy=True) | |
print(objDate.strftime(DATETIME_STR_FORMAT)) | |
my_image.datetime_original = objDate.strftime(DATETIME_STR_FORMAT) | |
my_image.set("datetime_digitized",objDate.strftime(DATETIME_STR_FORMAT)) | |
with open(target, 'wb') as new_image_file: | |
new_image_file.write(my_image.get_file()) | |
except Exception as e: | |
print(f"{e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment