Added EXIF location extractor

This commit is contained in:
Profitroll 2023-01-02 13:10:10 +01:00
parent aa15d17883
commit 075f08a8c1
2 changed files with 49 additions and 1 deletions

47
modules/exif_reader.py Normal file
View File

@ -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

View File

@ -7,3 +7,4 @@ opencv-python~=4.6.0.66
python-jose[cryptography]~=3.3.0
passlib~=1.7.4
apscheduler~=3.9.1.post1
exif==1.4.2