Skip to content

Instantly share code, notes, and snippets.

@paul2048
Created July 20, 2021 19:24
Show Gist options
  • Save paul2048/81f3696f56c7cd878b1752ce702ec23f to your computer and use it in GitHub Desktop.
Save paul2048/81f3696f56c7cd878b1752ce702ec23f to your computer and use it in GitHub Desktop.
When this script is executed in a directory that includes a "Google Photos" folder (which can be exported from here: https://takeout.google.com/settings/takeout/custom/photos), the images will have the correct creation date, rather than the date you downloaded the images. The code works only on Windows machines.
import os
import time
import json
from win32_setctime import setctime
ORIGINAL_FOLDER = 'Google Photos'
# Iterate through each album of your Google Photos
for album in os.listdir(ORIGINAL_FOLDER):
album_path = f'{ORIGINAL_FOLDER}/{album}'
try:
# Itereate through each file of the album
for file in os.listdir(album_path):
if file[-5:] != '.json':
file_path = f'{album_path}/{file}'
json_path = f'{file_path}.json'
try:
file_metadata = json.loads(open(json_path, 'r').read())
ts = int(file_metadata['photoTakenTime']['timestamp'])
# Update the creation date to the real creation date
setctime(file_path, ts)
# Update the `("access date", "modifying date")`
os.utime(file_path, times=(ts, ts))
# If the JSON file associated with the file doesn't exist
except FileNotFoundError:
print(f'Nonexistent: {json_path}')
# If the file name containts unicode characters (like Chinese letters)
except UnicodeDecodeError:
print(f'UnicodeDecodeError: {json_path}')
except NotADirectoryError:
print(f'NotADirectoryError: {album_path}')
print('DONE!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment