How to Extract GPS Coordinates from Images with Python’s exifread
This guide shows how to install the exifread library, read an image’s binary data in Python, extract its GPS latitude, longitude, altitude, and capture time, and then display the coordinates for location mapping.
Python’s exifread library can retrieve almost all EXIF metadata from an image, including GPS information. Install it with pip install exifread and then read the image as binary data.
The process reads the file, parses tags, extracts latitude, longitude, altitude, and the capture timestamp, and prints them in a readable format. If the image has been compressed, the binary EXIF data may be lost.
Example code:
import exifread
import re
# Read image as binary
f = open("luotuo.JPG", "rb")
tags = exifread.process_file(f)
# Containers for GPS data and capture time
GPS = {}
Data = ""
for tag, value in tags.items():
# Latitude
if re.match('GPS GPSLatitude', tag):
try:
match_result = re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]', str(value)).groups()
GPS['纬度'] = f"{int(match_result[0])} {int(match_result[1])} {int(match_result[2])/int(match_result[3])}"
except:
GPS['纬度'] = str(value)
# Longitude
elif re.match('GPS GPSLongitude', tag):
try:
match_result = re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]', str(value)).groups()
GPS['经度'] = f"{int(match_result[0])} {int(match_result[1])} {int(match_result[2])/int(match_result[3])}"
except:
GPS['经度'] = str(value)
# Altitude
elif re.match('GPS GPSAltitude', tag):
GPS['高度'] = str(value)
# Capture time
elif re.match('Image DateTime', tag):
Data = str(value)
print("纬 经 度:" + GPS['纬度'] + "," + GPS['经度'])
print("拍摄时间:" + Data)The script prints the latitude and longitude, which can be copied into any map service to locate the photo’s shooting spot.
Running the code on a sample camel photo taken in Sanya successfully outputs the GPS coordinates, confirming the image’s location.
Further extensions include using Baidu’s API to convert coordinates to a readable address, or simply viewing the details in the image’s file properties. To protect privacy, you can delete EXIF metadata via the image’s properties dialog.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
