Created
January 7, 2024 01:32
-
-
Save jtemporal/4669e0d6f32c5e9cdb81edd2cdb86079 to your computer and use it in GitHub Desktop.
Script to update exif data from .jpeg files
This file contains 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
""" | |
This script will automatically pick up on all JPG files in a folder | |
requirements.txt | |
piexif==1.1.3 | |
pillow==10.2.0 | |
""" | |
import json | |
import os | |
from datetime import datetime | |
import piexif | |
def get_metadata(file_name): | |
# read metadata file | |
return json.load(open(f"{file_name.split('.')[0]}.JPG.json")) | |
def get_correct_image_date(metadata): | |
# grabs correct image creation data | |
correct_creation_time = int(metadata['creationTime']['timestamp']) | |
correct_creation_time = datetime.fromtimestamp(correct_creation_time) | |
return f"{correct_creation_time.strftime('%Y:%m:%d %H:%M:%S')}".encode() | |
def update_exif_in_image(file_name, formatted_date): | |
print(f"Updating image {file_name}...") | |
# load image metadata | |
image_exif = piexif.load(file_name) | |
date_tags = [36867, 36868] | |
for dt in date_tags: | |
image_exif['Exif'][dt] = formatted_date | |
image_exif['0th'][306] = formatted_date | |
# image_exif['0th'][306] | |
image_exif_bytes = piexif.dump(image_exif) | |
piexif.insert(image_exif_bytes, file_name) | |
if __name__ == '__main__': | |
files = [f for f in os.listdir() if "jpeg" in f] | |
for f in files: | |
fmt_date = get_correct_image_date(get_metadata(f)) | |
update_exif_in_image(f, fmt_date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment