{"id":85,"date":"2023-03-09T15:23:34","date_gmt":"2023-03-09T15:23:34","guid":{"rendered":"https:\/\/smartsource.com.sg\/blog\/?p=85"},"modified":"2023-03-09T15:23:34","modified_gmt":"2023-03-09T15:23:34","slug":"get-location-from-an-image-using-python","status":"publish","type":"post","link":"https:\/\/smartsource.com.sg\/blog\/index.php\/2023\/03\/09\/get-location-from-an-image-using-python\/","title":{"rendered":"Get location from an image using Python"},"content":{"rendered":"\n<p>Here is the Python program to get location metadata from an image using the exifread library in Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import exifread\r\n\r\ndef get_location(image_path):\r\n    with open(image_path, 'rb') as f:\r\n        tags = exifread.process_file(f)\r\n        lat_ref = tags.get('GPS GPSLatitudeRef')\r\n        lat = tags.get('GPS GPSLatitude')\r\n        lon_ref = tags.get('GPS GPSLongitudeRef')\r\n        lon = tags.get('GPS GPSLongitude')\r\n        if lat and lat_ref and lon and lon_ref:\r\n            lat_val = convert_to_degrees(lat)\r\n            if lat_ref.values&#91;0] == 'S':\r\n                lat_val = -lat_val\r\n            lon_val = convert_to_degrees(lon)\r\n            if lon_ref.values&#91;0] == 'W':\r\n                lon_val = -lon_val\r\n            return (lat_val, lon_val)\r\n        else:\r\n            return None\r\n\r\ndef convert_to_degrees(value):\r\n    d = float(value.values&#91;0].num) \/ float(value.values&#91;0].den)\r\n    m = float(value.values&#91;1].num) \/ float(value.values&#91;1].den)\r\n    s = float(value.values&#91;2].num) \/ float(value.values&#91;2].den)\r\n    return d + (m \/ 60.0) + (s \/ 3600.0)\r\n\r\n# Example usage:\r\nlocation = get_location('image.jpg')\r\nif location:\r\n    print('Latitude:', location&#91;0])\r\n    print('Longitude:', location&#91;1])\r\nelse:\r\n    print('No location data found in image metadata.')\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is the Python program to get location metadata from an image using the exifread library in Python<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[62,63,64],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-image","tag-location","tag-python"],"_links":{"self":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":1,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions\/86"}],"wp:attachment":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}