Skip to content

Instantly share code, notes, and snippets.

@d0ugal
Created August 13, 2023 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0ugal/9f5208f6eece96b4f4a106c6948f9654 to your computer and use it in GitHub Desktop.
Save d0ugal/9f5208f6eece96b4f4a106c6948f9654 to your computer and use it in GitHub Desktop.
Adds the date info to a file based on the file name
from datetime import datetime
import piexif #piexif==1.1.3
import os
import sys
dir = sys.argv[1]
for file in os.listdir(dir):
if file.endswith('.png'):
continue
if file.endswith('.mp4'):
continue
print(file)
year, month, date, _ = file.split('_', 3)
filename =f"{dir}/{file}"
exif_dict = piexif.load(filename)
new_date = datetime(int(year), int(month), int(date)).strftime("%Y:%m:%d %H:%M:%S")
exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
print(exif_dict)
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment