Added EXIF location extractor
This commit is contained in:
parent
aa15d17883
commit
075f08a8c1
47
modules/exif_reader.py
Normal file
47
modules/exif_reader.py
Normal 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
|
@ -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
|
Loading…
Reference in New Issue
Block a user