Browse Source

startup: add function "read_exif_datetime_original"

Fabian Peter Hammerle 2 weeks ago
parent
commit
19ba9b43e4
2 changed files with 16 additions and 0 deletions
  1. 2 0
      README.md
  2. 14 0
      profile_default/startup/init.py

+ 2 - 0
README.md

@@ -1,5 +1,7 @@
 ```sh
 sudo apt-get install --no-install-recommends \
+    python3-dateutil \
+    python3-exifread \
     python3-pgpdump \
     python3-pyperclip \
     python3-sympy

+ 14 - 0
profile_default/startup/init.py

@@ -1,3 +1,4 @@
+import datetime
 import functools
 import itertools
 import os
@@ -5,6 +6,8 @@ import pathlib
 import typing
 import warnings
 
+import dateutil.parser
+import exifread
 import numpy
 import pandas
 import pgpdump
@@ -42,6 +45,17 @@ def numpy_array_from_file(
     return numpy.frombuffer(path.read_bytes(), dtype=dtype)
 
 
+def read_exif_datetime_original(path: str) -> datetime.datetime:
+    with pathlib.Path(path).open("rb") as file:
+        tags = exifread.process_file(file)
+        return dateutil.parser.parse(
+            tags["EXIF DateTimeOriginal"].values
+            + "."
+            + tags["EXIF SubSecTimeOriginal"].values
+            + tags["EXIF OffsetTimeOriginal"].values
+        )
+
+
 def split_pgp_file(
     path: pathlib.Path,
 ) -> typing.Iterator[typing.Union[bytearray, pgpdump.packet.Packet]]: