From 075f08a8c1379f8fddef5d05a39e1f6869485676 Mon Sep 17 00:00:00 2001 From: profitroll Date: Mon, 2 Jan 2023 13:10:10 +0100 Subject: [PATCH] Added EXIF location extractor --- modules/exif_reader.py | 47 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 modules/exif_reader.py diff --git a/modules/exif_reader.py b/modules/exif_reader.py new file mode 100644 index 0000000..04c7972 --- /dev/null +++ b/modules/exif_reader.py @@ -0,0 +1,47 @@ +from exif import Image + +def decimal_coords(coords: float, ref: str) -> float: + """Get latitude/longitude from coord and direction reference + + ### Args: + * coords (`float`): _description_ + * ref (`str`): _description_ + + ### Returns: + * float: Decimal degrees + """ + decimal_degrees = coords[0] + coords[1] / 60 + coords[2] / 3600 + if ref == "S" or ref == "W": + decimal_degrees = -decimal_degrees + return decimal_degrees + +def extract_location(filepath: str) -> dict: + """Get location data from image + + ### Args: + * filepath (`str`): Path to file location + + ### Returns: + * dict: `{ "latitude": float, "longitude": float, "altitude": float }` + """ + + output = { + "latitude": 0.0, + "longitude": 0.0, + "altitude": 0.0 + } + + with open(filepath, 'rb') as src: + img = Image(src) + + if img.has_exif is False: + return output + + try: + output["latitude"] = decimal_coords(img.gps_latitude, img.gps_latitude_ref) + output["longitude"] = decimal_coords(img.gps_longitude, img.gps_longitude_ref) + output["altitude"] = img.gps_altitude + except AttributeError: + pass + + return output \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 04a0283..d13fc27 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ python-magic~=0.4.27 opencv-python~=4.6.0.66 python-jose[cryptography]~=3.3.0 passlib~=1.7.4 -apscheduler~=3.9.1.post1 \ No newline at end of file +apscheduler~=3.9.1.post1 +exif==1.4.2 \ No newline at end of file