Created
October 9, 2021 11:45
-
-
Save andiwand/87d69f5919c2fa1f8aaf160f4694fd4b to your computer and use it in GitHub Desktop.
fix google photo dates coming from whatsapp backup
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
import os | |
import re | |
import argparse | |
import piexif | |
from datetime import datetime, timedelta | |
parser = argparse.ArgumentParser() | |
parser.add_argument('path') | |
args = parser.parse_args() | |
for path in [os.path.join(args.path, path) for path in os.listdir(args.path) if os.path.isfile(os.path.join(args.path, path))]: | |
name = os.path.basename(path) | |
strings = re.split(';|,|\.|_|-', name) | |
date = None | |
for string in strings: | |
try: | |
date = datetime.strptime(string, '%Y%m%d') | |
break | |
except ValueError: | |
pass | |
if date is None: | |
print(f'could not read date from name {name}') | |
date_string = date.strftime('%Y:%m:%d %H:%M:%S') | |
try: | |
exif = piexif.load(path) | |
exif['Exif'][piexif.ExifIFD.DateTimeOriginal] = date_string | |
exif['Exif'][piexif.ExifIFD.DateTimeDigitized] = date_string | |
piexif.insert(piexif.dump(exif), path) | |
except: | |
print(f'could not set date for file {name}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment