gpx-to-geotags/lib/image.py

24 lines
594 B
Python

from datetime import datetime
import pytz
from PIL.ExifTags import TAGS
def get_image_timestamp(img, timezone):
exif = img._getexif()
if not exif:
raise ValueError("No EXIF metadata found")
found_timestamp = None
for (idx, tag) in TAGS.items():
if tag == 'DateTime':
found_timestamp = exif.get(idx)
elif tag == 'DateTimeOriginal':
found_timestamp = exif.get(idx)
if found_timestamp:
dt = datetime.strptime(found_timestamp, '%Y:%m:%d %H:%M:%S')
tz = pytz.timezone(timezone)
return tz.localize(dt)