Skip to content

Instantly share code, notes, and snippets.

@masasin
Last active March 9, 2024 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masasin/5075fec5d4a55400c460c00d4c398e85 to your computer and use it in GitHub Desktop.
Save masasin/5075fec5d4a55400c460c00d4c398e85 to your computer and use it in GitHub Desktop.
Google Photos uses `datetime_original` EXIF tag to sort photos. If using a physical camera etc, the times can be wrong. Fix them by running this with the `exif` library on PyPI.
import datetime as dt
from pathlib import Path
from typing import NoReturn
from exif import Image
TIMEDELTA = -dt.timedelta(hours=8, minutes=12, seconds=10)
DATE_FORMAT = "%Y:%m:%d %H:%M:%S" # Verify
def process_photo(file: Path, timedelta: dt.timedelta) -> NoReturn:
with open(file, "rb") as f:
img = Image(f)
original = dt.datetime.strptime(img.datetime, DATE_FORMAT)
adjusted = original + timedelta
img.datetime = adjusted.strftime(DATE_FORMAT)
img.datetime_original = img.datetime
img.datetime_digitized = img.datetime
with open(file, "wb") as f:
f.write(img.get_file())
if __name__ == "__main__":
for file in Path(".").glob("*.jpg"):
process_photo(file, TIMEDELTA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment