rename your photos to their DateTimeOriginal datetime in EXIF metadata.
Last active
June 9, 2023 19:30
-
-
Save pirafrank/4055fa3073b42c50659fdf7d2a3e9d3a to your computer and use it in GitHub Desktop.
exif_renamer - EXIF to 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
bin | |
include | |
lib | |
lib64 | |
target | |
pyvenv.cfg |
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
3.10.4 |
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
piexif==1.1.3 |
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
import sys | |
import os | |
import fnmatch | |
from datetime import datetime | |
import piexif | |
def filter_files_with_patterns(directory, patterns): | |
matching_files = [] | |
for pattern in patterns: | |
matching_files.extend(fnmatch.filter(os.listdir(directory), pattern)) | |
return matching_files | |
def rename_with_exif_data(directory): | |
patterns = ['*.jpg', '*.jpeg', '*.JPG', '*.JPEG'] | |
total_files = len(filter_files_with_patterns(directory, patterns)) | |
total_errors = 0 | |
for i, filename in enumerate(os.listdir(directory)): | |
if filename.lower().endswith(('.jpg', '.jpeg')): | |
print(f"Processing {i+1}/{total_files}, filename: {filename}") | |
file_path = os.path.join(directory, filename) | |
try: | |
exif_data = piexif.load(file_path) | |
if exif_data['Exif'] is not None: | |
date_time = exif_data['Exif'][piexif.ExifIFD.DateTimeOriginal] | |
if date_time is not None: | |
dt = datetime.strptime(date_time.decode('ascii'), '%Y:%m:%d %H:%M:%S') | |
extension = os.path.splitext(filename)[1] | |
new_filename = dt.strftime('%Y%m%d_%H%M%S_nikon') + extension | |
new_file_path = os.path.join(directory, new_filename) | |
os.rename(file_path, new_file_path) | |
print(f"Renamed '{filename}' to '{new_filename}'") | |
else: | |
print("Oh no, EXIF DateTimeOriginal not found") | |
else: | |
print("Oh no, exit data NOT found") | |
except (IOError, OSError, KeyError): | |
total_errors += 1 | |
print(f"ERROR: Failed to process '{filename}'") | |
print(f"Finished with {total_errors} error/s") | |
# main script | |
if len(sys.argv) > 1: | |
DIRECTORY_PATH = sys.argv[1] | |
rename_with_exif_data(DIRECTORY_PATH) | |
else: | |
print("ERROR: No argument provided.") | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment