MCPcopy Index your code
hub / github.com/geekcomputers/Python / exif_datetime_original

Function exif_datetime_original

photo_timestamp_renamer.py:49–85  ·  view source on GitHub ↗

Try to read EXIF DateTimeOriginal/DateTime from image. Returns None if unavailable.

(path: Path)

Source from the content-addressed store, hash-verified

47
48
49def exif_datetime_original(path: Path) -> datetime | None:
50 """
51 Try to read EXIF DateTimeOriginal/DateTime from image.
52 Returns None if unavailable.
53 """
54 if not PIL_OK:
55 return None
56 try:
57 img = Image.open(path)
58 exif = img.getexif()
59 if not exif:
60 return None
61
62 # map EXIF tag ids -> names
63 tag_map = {}
64 for k, v in ExifTags.TAGS.items():
65 tag_map[k] = v
66
67 # common EXIF datetime tags
68 dto = None
69 dt = None
70 for tag_id, value in exif.items():
71 name = tag_map.get(tag_id)
72 if name == "DateTimeOriginal":
73 dto = value
74 elif name == "DateTime":
75 dt = value
76
77 raw = dto or dt
78 if not raw:
79 return None
80
81 # EXIF datetime format: "YYYY:MM:DD HH:MM:SS"
82 raw = str(raw).strip()
83 return datetime.strptime(raw, "%Y:%m:%d %H:%M:%S")
84 except Exception:
85 return None
86
87
88def file_mtime(path: Path) -> datetime:

Callers 1

rename_photosFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected