Last active
March 9, 2024 13:41
-
-
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.
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 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