Professional Documents
Culture Documents
Oct 2021
Oct 2021
import shutil
from PIL import Image
from datetime import datetime
# Function to get the earliest date from the file system and EXIF data
def get_earliest_date(file_path):
# Get 'Date Created' and 'Date Modified' from the file system
stat = os.stat(file_path)
date_created = datetime.fromtimestamp(stat.st_ctime) # Creation time
date_modified = datetime.fromtimestamp(stat.st_mtime) # Last modified time
# Function to organize images into folders based on the earliest of the dates
def organize_images_by_date(image_directory):
for filename in os.listdir(image_directory):
if filename.lower().endswith((".jpg", ".png", ".jpeg")): # Include other
image formats if necessary
file_path = os.path.join(image_directory, filename)
earliest_date = get_earliest_date(file_path)
if earliest_date:
# Format the folder name as 'Jan, 2013'
month_year_folder = earliest_date.strftime("%b, %Y")
target_directory = os.path.join(image_directory, month_year_folder)