How to Change the "Date Taken" Metadata for All Image Files in a Folder Using Python
Manipulating image metadata can be crucial for various reasons, such as organizing photos, correcting incorrect timestamps, or preparing images for uploading to certain platforms. In this blog post, we will walk through a simple Python script that allows you to change the "date taken" metadata for all JPEG images in a specific folder.
#### Prerequisites
Before we dive into the script, make sure you have Python installed on your system. If not, you can download it from the [official Python website](https://www.python.org/).
Additionally, you'll need to install two Python libraries: `Pillow` and `piexif`. These libraries will help us handle image files and manipulate their EXIF metadata, respectively.
You can install these libraries using `pip`. Open your terminal or command prompt and run the following commands:
```bash
pip install Pillow piexif
```
#### The Python Script
Here's the complete Python script that changes the "date taken" metadata for all JPEG images in a specified folder:
```python
import os
from PIL import Image
import piexif
def change_date_taken(folder_path, new_date):
for filename in os.listdir(folder_path):
if filename.lower().endswith(('jpg', 'jpeg')):
file_path = os.path.join(folder_path, filename)
try:
# Open the image file
image = Image.open(file_path)
exif_dict = piexif.load(image.info.get('exif', b""))
# Set the new date
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
# Convert the exif dictionary back to bytes
exif_bytes = piexif.dump(exif_dict)
# Save the image with new EXIF data
image.save(file_path, "jpeg", exif=exif_bytes)
print(f"Updated date taken for {filename}")
except Exception as e:
print(f"Failed to update {filename}: {e}")
# Example usage
folder_path = r'C:\Users\alexc\Pictures\iCloud Photos\Photos\2010 - Switzerland'
new_date = "2024:01:01 00:00:00" # Format: YYYY:MM:DD HH:MM:SS
change_date_taken(folder_path, new_date)
```
### How It Works
1. **Import Necessary Libraries:**
- `os` is used to navigate through the folder and list the files.
- `PIL` (Pillow) is used to open and save image files.
- `piexif` is used to load, manipulate, and save EXIF data.
2. **Define the Function:**
- `change_date_taken(folder_path, new_date)` is the function that performs the task. It takes two parameters: the path to the folder containing the images and the new date string in the format `YYYY:MM:DD HH:MM:SS`.
3. **Iterate Through Files:**
- The script iterates through all files in the specified folder. It checks if each file is a JPEG or JPG image.
4. **Open Image and Load EXIF Data:**
- It opens the image and loads its EXIF data using `piexif.load()`. If the image does not have EXIF data, it loads an empty dictionary to prevent errors.
5. **Update Date Fields:**
- The script updates the `DateTimeOriginal` and `DateTimeDigitized` fields in the EXIF data with the new date provided.
6. **Save the Image:**
- The updated EXIF data is converted back to bytes and saved to the image file using `image.save()`.
### Running the Script
To run the script, save it to a file, for example, `change_date_taken.py`. Update the `folder_path` and `new_date` variables with your specific folder path and desired date. Then, run the script from your terminal or command prompt:
```bash
python change_date_taken.py
```
### Conclusion
This script is a handy tool for anyone needing to batch update the "date taken" metadata of their JPEG images. With Python and a couple of libraries, you can automate this process and save yourself a lot of manual work. Happy coding!