Renaming photos by date using Python is a common task that can be automated efficiently by reading the metadata (EXIF data) of the images to extract the date they were taken, then renaming the files accordingly. Here’s a detailed guide and a complete Python script to do this.
How to Rename Photos by Date in Python
Key Steps:
-
Read image files from a folder
-
Extract the date the photo was taken (usually stored in EXIF metadata)
-
Format the date into a filename-friendly string
-
Rename the files based on this date
Required Libraries
-
Pillow (
PIL
): To read EXIF metadata from images -
os: To handle file system operations
-
shutil (optional): To move files if needed
Install Pillow if you don’t have it:
Python Script to Rename Photos by Date
Explanation:
-
get_date_taken()
: Opens the image and reads theDateTimeOriginal
EXIF tag, which usually holds when the photo was taken. -
format_date()
: Converts the EXIF date string fromYYYY:MM:DD HH:MM:SS
to a filename-friendly formatYYYY-MM-DD_HH-MM-SS
. -
rename_photos_by_date()
: Iterates over supported image files, renames them using the date, and handles duplicate names by appending a counter. -
The script prompts you for the folder path where the images are stored.
Notes:
-
Some images may lack EXIF data or the
DateTimeOriginal
tag, especially screenshots or images downloaded from the web. -
You can extend the script to use file creation or modification time as a fallback if no EXIF date is found.
-
This script currently supports common photo file extensions; add or remove extensions as needed.
If you want, I can help you modify the script to include fallback methods or to handle videos too!