Skip to content

Instantly share code, notes, and snippets.

@adkdev
Created August 2, 2021 08:59
Show Gist options
  • Save adkdev/f979974153897736bfe7b9a53e6485ca to your computer and use it in GitHub Desktop.
Save adkdev/f979974153897736bfe7b9a53e6485ca to your computer and use it in GitHub Desktop.
Add Exif date for the missing
from datetime import datetime
from os import listdir
from os.path import isfile, join
import piexif
img_dir = "./img/"
files_in_dir = [f for f in listdir(img_dir) if isfile(join(img_dir, f))]
for file in files_in_dir:
filename = f"{img_dir}/{file}"
print(filename)
exif_dict = piexif.load(filename)
print(exif_dict)
print()
if not exif_dict.get("Exif"):
file_date = " ".join(filename.split("_")[1:3])
new_date = datetime.strptime(file_date, "%Y%m%d %H%M%S")
# convert to exif date format
exif_date = new_date.strftime("%Y:%m:%d %H:%M:%S")
exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = exif_date
exif_dict["Exif"][piexif.ExifIFD.DateTimeDigitized] = exif_date
print(exif_dict)
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)
else:
print("pass")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment