Last active
September 16, 2025 07:52
-
-
Save Kiougar/fbdf494f4296be4e3e95ea7e4506d577 to your computer and use it in GitHub Desktop.
Python script that updates dates for image files taken with the wrong date set on the device
This file contains hidden or 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 piexif | |
| import os | |
| from datetime import datetime, timedelta | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| # directory that contains the image files we want to change | |
| IMAGE_DIR = os.path.join(BASE_DIR, "YOUR_IMAGE_DIR") | |
| # offset applied to current date on every image file | |
| OFFSET = timedelta(days=1394, hours=1) | |
| def print_exif_data(filename): | |
| filepath = os.path.join(IMAGE_DIR, filename) | |
| print(f"--- EXIF data for {filename} ---") | |
| try: | |
| exif_dict = piexif.load(filepath) | |
| if not exif_dict or all(not v for v in exif_dict.values()): | |
| print("No EXIF data found.") | |
| else: | |
| for ifd_name in exif_dict: | |
| if ifd_name == "thumbnail": | |
| continue | |
| print(f"\n{ifd_name}:") | |
| for key, val in exif_dict[ifd_name].items(): | |
| print(f" {piexif.TAGS[ifd_name][key]['name']} ({key}): {val}") | |
| except Exception as e: | |
| print(f"Could not read EXIF data. Reason: {e}") | |
| print("-" * (len(filename) + 20) + "\n") | |
| def change_date_data(filename, dry_run=True): | |
| filepath = os.path.join(IMAGE_DIR, filename) | |
| print(f"--- Changing date data for {filename} ---") | |
| try: | |
| exif_dict = piexif.load(filepath) | |
| if not exif_dict or all(not v for v in exif_dict.values()): | |
| print("No EXIF data found.") | |
| return | |
| except Exception as e: | |
| print(f" [ERROR] Could not read EXIF data. Reason: {e}") | |
| return | |
| try: | |
| curr_data = exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal].decode("utf-8") | |
| dt = datetime.strptime(curr_data, "%Y:%m:%d %H:%M:%S") | |
| except Exception as e: | |
| print(f" [ERROR] Could not parse current date. Reason: {e}") | |
| return | |
| new_dt = dt + OFFSET | |
| new_data = new_dt.strftime("%Y:%m:%d %H:%M:%S") | |
| if dry_run: | |
| print(f" Would change from {curr_data} to {new_data}") | |
| else: | |
| exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = new_data | |
| exif_dict["Exif"][piexif.ExifIFD.DateTimeDigitized] = new_data | |
| exif_bytes = piexif.dump(exif_dict) | |
| piexif.insert(exif_bytes, filepath) | |
| print(f" changed from {curr_data} to {new_data}") | |
| print("-" * (len(filename) + 31) + "\n") | |
| def main(dry_run=True): | |
| if not os.path.isdir(IMAGE_DIR): | |
| print(f"Error: Directory not found at '{IMAGE_DIR}'") | |
| return | |
| print(f"Reading images from: {IMAGE_DIR}") | |
| for filename in os.listdir(IMAGE_DIR): | |
| if not filename.lower().endswith((".jpg", ".jpeg")): | |
| continue | |
| change_date_data(filename, dry_run) | |
| if __name__ == "__main__": | |
| # Change to false when ready to update date | |
| main(dry_run=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment