Created
February 18, 2021 02:03
-
-
Save isosphere/0711da9c7d9e59108bd31f78ca3aaa57 to your computer and use it in GitHub Desktop.
A tool to correct the stripped datetime of Google Takeout image exports
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 datetime | |
import json | |
import os | |
from exif import Image | |
google_photos_directory = r'C:\\Users\\Matt\\Downloads\\Takeout\\Google Photos' | |
database = dict() | |
for root, dirs, files in os.walk(google_photos_directory): | |
for name in files: | |
full_path = os.path.join(root, name) | |
filename, file_extension = os.path.splitext(name) | |
file_extension = file_extension[1:].lower() | |
if file_extension not in ('jpg', 'json'): | |
continue | |
if filename not in database: | |
database[filename] = {} | |
if file_extension == 'jpg': | |
database[filename]['path'] = full_path | |
elif file_extension == 'json': | |
with open(full_path) as file_handle: | |
metadata = json.load(file_handle) | |
if not metadata or 'albumData' in metadata: | |
continue | |
file_creation = datetime.datetime.utcfromtimestamp(int(metadata['photoTakenTime']['timestamp'])) | |
database[filename]['timestamp'] = file_creation | |
for filename in database: | |
if not database[filename] or 'timestamp' not in database[filename] or 'path' not in database[filename]: | |
continue | |
my_image = None | |
with open(database[filename]['path'], 'rb') as image_file: | |
my_image = Image(image_file) | |
my_image.datetime = database[filename]['timestamp'].strftime("%Y:%m:%d %H:%M:%S") | |
with open(database[filename]['path'], 'wb') as image_file: | |
image_file.write(my_image.get_file()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment