Skip to content

Instantly share code, notes, and snippets.

@alea12
Created October 10, 2024 14:55
Show Gist options
  • Save alea12/a3cffaf274eefc0b5f0f19a7ead6b9e5 to your computer and use it in GitHub Desktop.
Save alea12/a3cffaf274eefc0b5f0f19a7ead6b9e5 to your computer and use it in GitHub Desktop.
exif_folder_renamer.py
import os
from PIL import Image
from PIL.ExifTags import TAGS
# Function to check if the image was taken by a Fujifilm camera
def is_fujifilm(image_path):
try:
image = Image.open(image_path)
exif_data = image._getexif()
if exif_data:
for tag, value in exif_data.items():
decoded_tag = TAGS.get(tag, tag)
if decoded_tag == "Make" and "Fujifilm" in value:
return True
except Exception as e:
print(f"Error reading EXIF data from {image_path}: {e}")
return False
# Main function to process directories
def process_directories(base_directory):
for root, dirs, files in os.walk(base_directory):
for directory in dirs:
if ' ' in directory: # Skip directories with spaces
continue
dir_path = os.path.join(root, directory)
image_count = 0
has_fujifilm = False
# Count image files and check if any file is taken by Fujifilm
for filename in os.listdir(dir_path):
if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
image_count += 1
if not has_fujifilm:
image_path = os.path.join(dir_path, filename)
if is_fujifilm(image_path):
has_fujifilm = True
# Skip renaming if there are no images
if image_count > 0:
new_folder_name = f"{directory} FUJI {image_count}" if has_fujifilm else f"{directory} {image_count}"
new_dir_path = os.path.join(root, new_folder_name)
# Rename the folder
try:
os.rename(dir_path, new_dir_path)
print(f"Renamed: {dir_path} -> {new_dir_path}")
except Exception as e:
print(f"Error renaming {dir_path}: {e}")
break # Prevent recursive walk, only process the top-level directories
if __name__ == "__main__":
# Replace with the directory where this script is located
base_directory = os.path.dirname(os.path.abspath(__file__))
process_directories(base_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment