Created
February 5, 2024 09:00
-
-
Save kairat-beep/a56c3509ee60805803e562c4174ef6b7 to your computer and use it in GitHub Desktop.
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
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