Skip to content

Instantly share code, notes, and snippets.

@kairat-beep
Created February 5, 2024 09:00
Show Gist options
  • Save kairat-beep/a56c3509ee60805803e562c4174ef6b7 to your computer and use it in GitHub Desktop.
Save kairat-beep/a56c3509ee60805803e562c4174ef6b7 to your computer and use it in GitHub Desktop.
from PIL import Image
from PIL.ExifTags import TAGS
import glob
import os
from datetime import datetime
folder= './Combined'
def get_exif_date_taken(file_path):
try:
with Image.open(file_path) as img:
exif_data = img._getexif()
if exif_data is not None:
for tag, value in exif_data.items():
tag_name = TAGS.get(tag, tag)
if tag_name == "DateTimeOriginal":
return datetime.strptime(value, "%Y:%m:%d %H:%M:%S"),str(img.format)
except Exception as e:
print(f"Error getting EXIF data for {file_path}: {e}")
return None,None
for pathstr in glob.glob(glob.escape(folder) + '/**/*', recursive=True):
new_name,format =get_exif_date_taken(pathstr)
if new_name and isinstance(new_name,datetime):
new_name = str(new_name)
os.rename(pathstr,pathstr.replace(os.path.basename(pathstr), f'{new_name}.{format}'))
#There might be more elegant way, even 1 liner, but i am famous for writing a mess.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment