{"id":14659,"date":"2024-03-15T09:42:12","date_gmt":"2024-03-15T09:42:12","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/"},"modified":"2025-08-06T11:42:17","modified_gmt":"2025-08-06T11:42:17","slug":"how-can-python-retrieve-geographical-location-information-from-photos","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/","title":{"rendered":"Extract Photo Geolocation with Python"},"content":{"rendered":"<p>To obtain the geographic location information of a photo, you can use either the PIL library (Python Imaging Library) or the ExifRead library in Python.<\/p>\n<p>To install the PIL and ExifRead libraries, you can use the following command:<\/p>\n<pre class=\"post-pre\"><code>pip install pillow\r\npip install exifread\r\n<\/code><\/pre>\n<p>Next, you can use the following code to obtain the geographical location information of the photo:<\/p>\n<p>Utilize the PIL library:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image\r\n<span class=\"hljs-keyword\">from<\/span> PIL.ExifTags <span class=\"hljs-keyword\">import<\/span> TAGS\r\n\r\n<span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title function_\">get_geolocation<\/span>(<span class=\"hljs-params\">image_path<\/span>):\r\n    image = Image.<span class=\"hljs-built_in\">open<\/span>(image_path)\r\n    exif_data = image._getexif()\r\n    \r\n    <span class=\"hljs-keyword\">if<\/span> exif_data <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\r\n        <span class=\"hljs-keyword\">for<\/span> tag_id, value <span class=\"hljs-keyword\">in<\/span> exif_data.items():\r\n            tag_name = TAGS.get(tag_id, tag_id)\r\n            <span class=\"hljs-keyword\">if<\/span> tag_name == <span class=\"hljs-string\">'GPSInfo'<\/span>:\r\n                <span class=\"hljs-keyword\">return<\/span> value\r\n\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">None<\/span>\r\n\r\n<span class=\"hljs-comment\"># \u4f20\u5165\u7167\u7247\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570<\/span>\r\ngeolocation = get_geolocation(<span class=\"hljs-string\">'path\/to\/photo.jpg'<\/span>)\r\n<span class=\"hljs-keyword\">if<\/span> geolocation <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\r\n    <span class=\"hljs-built_in\">print<\/span>(geolocation)\r\n<span class=\"hljs-keyword\">else<\/span>:\r\n    <span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'No geolocation found.'<\/span>)\r\n<\/code><\/pre>\n<p>Utilize the ExifRead library.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">import<\/span> exifread\r\n\r\n<span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title function_\">get_geolocation<\/span>(<span class=\"hljs-params\">image_path<\/span>):\r\n    <span class=\"hljs-keyword\">with<\/span> <span class=\"hljs-built_in\">open<\/span>(image_path, <span class=\"hljs-string\">'rb'<\/span>) <span class=\"hljs-keyword\">as<\/span> image_file:\r\n        tags = exifread.process_file(image_file)\r\n        \r\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-string\">'GPS GPSLatitude'<\/span> <span class=\"hljs-keyword\">in<\/span> tags <span class=\"hljs-keyword\">and<\/span> <span class=\"hljs-string\">'GPS GPSLongitude'<\/span> <span class=\"hljs-keyword\">in<\/span> tags:\r\n            latitude = tags[<span class=\"hljs-string\">'GPS GPSLatitude'<\/span>].values\r\n            longitude = tags[<span class=\"hljs-string\">'GPS GPSLongitude'<\/span>].values\r\n            <span class=\"hljs-keyword\">return<\/span> (latitude, longitude)\r\n\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">None<\/span>\r\n\r\n<span class=\"hljs-comment\"># \u4f20\u5165\u7167\u7247\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570<\/span>\r\ngeolocation = get_geolocation(<span class=\"hljs-string\">'path\/to\/photo.jpg'<\/span>)\r\n<span class=\"hljs-keyword\">if<\/span> geolocation <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\r\n    <span class=\"hljs-built_in\">print<\/span>(geolocation)\r\n<span class=\"hljs-keyword\">else<\/span>:\r\n    <span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'No geolocation found.'<\/span>)\r\n<\/code><\/pre>\n<p>These codes will display the geographical location information of the photos. If there is no geographical location information in the photo, it will output &#8216;No geolocation found.&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To obtain the geographic location information of a photo, you can use either the PIL library (Python Imaging Library) or the ExifRead library in Python. To install the PIL and ExifRead libraries, you can use the following command: pip install pillow pip install exifread Next, you can use the following code to obtain the geographical [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[19800,19801,19802,18434,19799],"class_list":["post-14659","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-exif-data-extraction","tag-geolocation-api","tag-metadata-python","tag-pil-library","tag-python-photo-location"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Extract Photo Geolocation with Python - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to extract GPS data from photos using Python with PIL\/ExifRead libraries. Simple Python geolocation tutorial.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Extract Photo Geolocation with Python\" \/>\n<meta property=\"og:description\" content=\"Learn how to extract GPS data from photos using Python with PIL\/ExifRead libraries. Simple Python geolocation tutorial.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T09:42:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T11:42:17+00:00\" \/>\n<meta name=\"author\" content=\"Sophia Anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sophia Anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\"},\"author\":{\"name\":\"Sophia Anderson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30\"},\"headline\":\"Extract Photo Geolocation with Python\",\"datePublished\":\"2024-03-15T09:42:12+00:00\",\"dateModified\":\"2025-08-06T11:42:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\"},\"wordCount\":94,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"EXIF data extraction\",\"geolocation API\",\"metadata Python\",\"PIL Library\",\"Python photo location\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\",\"name\":\"Extract Photo Geolocation with Python - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T09:42:12+00:00\",\"dateModified\":\"2025-08-06T11:42:17+00:00\",\"description\":\"Learn how to extract GPS data from photos using Python with PIL\/ExifRead libraries. Simple Python geolocation tutorial.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Extract Photo Geolocation with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30\",\"name\":\"Sophia Anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g\",\"caption\":\"Sophia Anderson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/sophiaanderson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Extract Photo Geolocation with Python - Blog - Silicon Cloud","description":"Learn how to extract GPS data from photos using Python with PIL\/ExifRead libraries. Simple Python geolocation tutorial.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/","og_locale":"en_US","og_type":"article","og_title":"Extract Photo Geolocation with Python","og_description":"Learn how to extract GPS data from photos using Python with PIL\/ExifRead libraries. Simple Python geolocation tutorial.","og_url":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T09:42:12+00:00","article_modified_time":"2025-08-06T11:42:17+00:00","author":"Sophia Anderson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Sophia Anderson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/"},"author":{"name":"Sophia Anderson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30"},"headline":"Extract Photo Geolocation with Python","datePublished":"2024-03-15T09:42:12+00:00","dateModified":"2025-08-06T11:42:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/"},"wordCount":94,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["EXIF data extraction","geolocation API","metadata Python","PIL Library","Python photo location"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/","url":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/","name":"Extract Photo Geolocation with Python - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T09:42:12+00:00","dateModified":"2025-08-06T11:42:17+00:00","description":"Learn how to extract GPS data from photos using Python with PIL\/ExifRead libraries. Simple Python geolocation tutorial.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-retrieve-geographical-location-information-from-photos\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Extract Photo Geolocation with Python"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30","name":"Sophia Anderson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g","caption":"Sophia Anderson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/sophiaanderson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=14659"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14659\/revisions"}],"predecessor-version":[{"id":158697,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14659\/revisions\/158697"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=14659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=14659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=14659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}