From 0eb607786b282bdc85485456b2078ebc897272c1 Mon Sep 17 00:00:00 2001 From: Lea Date: Sun, 10 Sep 2023 23:31:37 +0200 Subject: [PATCH] =?UTF-8?q?Z=C3=ADsk=C3=A1n=C3=AD=20textov=C3=A9=20podoby?= =?UTF-8?q?=20lokality=20z=20GPS=20metadat=20obr=C3=A1zku?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ get_locality.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 6 ++++ 3 files changed, 87 insertions(+) create mode 100644 README.md create mode 100644 get_locality.py create mode 100644 requirements.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..d015536 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +### Usage + +```bash +$ python get_locality.py -i \ No newline at end of file diff --git a/get_locality.py b/get_locality.py new file mode 100644 index 0000000..e436c51 --- /dev/null +++ b/get_locality.py @@ -0,0 +1,77 @@ +import sys +from PIL import Image +from PIL.ExifTags import TAGS, GPSTAGS +import requests + +API_KEY = "LSoLu_4Ca6jjJPVaBZnNPdObHi701oGYbZmnyukL_4I" + +def get_decimal_form_dms(dms, ref): + degrees = dms[0] + minutes = dms[1] / 60.0 + seconds = dms[2] / 3600.0 + + if ref in ['S', 'W']: + degrees = -degrees + minutes = -minutes + seconds = -seconds + + return round(degrees + minutes + seconds, 5) + +# define function get_geodata which extracts geolocation data from image metadata and converts it to decimal degrees +def get_geodata(exif_data): + latitude = None + latitude_ref = None + longitude = None + longitude_ref = None + + for tag, value in exif_data.items(): + if TAGS.get(tag) == 'GPSInfo': + for key in value.keys(): + if GPSTAGS.get(key) == 'GPSLatitude': + latitude = value[key] + elif GPSTAGS.get(key) == 'GPSLatitudeRef': + latitude_ref = value[key] + elif GPSTAGS.get(key) == 'GPSLongitude': + longitude = value[key] + elif GPSTAGS.get(key) == 'GPSLongitudeRef': + longitude_ref = value[key] + latitude = get_decimal_form_dms(latitude, latitude_ref) + longitude = get_decimal_form_dms(longitude, longitude_ref) + return latitude, longitude + +def get_location_info(latitude, longitude): + if not latitude or not longitude: + return None, None + url = f"https://api.mapy.cz/v1/rgeocode?lon={longitude}&lat={latitude}&apikey={API_KEY}&lang=cs" + response = requests.get(url) + + if response.status_code == 200: + location_data = response.json() + items = location_data.get('items', []) + if items: + item = items[0] + locality = item['location'] + region = item['regionalStructure'][5]['name'] + + return locality, region + return None, None + +# get picture file name from command line argument +def main(): + if len(sys.argv) != 3 or sys.argv[1] != '-i': + print("Chybné použití. Použijte: script.py -i vstupni_obrazek") + sys.exit(1) + + input_image_path = sys.argv[2] + + try: + image = Image.open(input_image_path) + exif_data = image._getexif() + except Exception as e: + print(f"Chyba při čtení metadat obrázku: {str(e)}") + sys.exit(1) + + print(f'Lokalita: {get_location_info(*get_geodata(exif_data))}') + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7748840 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +certifi==2023.7.22 +charset-normalizer==3.2.0 +idna==3.4 +Pillow==10.0.0 +requests==2.31.0 +urllib3==2.0.4