Fundamentals 4 min read

Extracting GPS Information from Images Using Python exifread

This tutorial demonstrates how to install the exifread library, read an image's binary data in Python, extract GPS coordinates and other EXIF metadata, and print the results, while also noting image compression effects and possible extensions such as reverse‑geocoding.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Extracting GPS Information from Images Using Python exifread

This article introduces the exifread Python library for extracting EXIF metadata, especially GPS information, from image files. After installing the library with pip install exifread , the script opens an image in binary mode, processes the file, and iterates over the tags to locate latitude, longitude, altitude, and capture time.

The extracted latitude and longitude are converted from rational numbers to decimal format, handling potential parsing errors with fallback values. The script then prints the coordinates and the capture time, allowing users to copy the coordinates into a map service for location lookup.

Images compressed or stripped of metadata will not yield GPS data, and the article notes that the same information can be viewed via the image's property details in the operating system. It also suggests using external APIs (e.g., Baidu) to reverse‑geocode the coordinates into a human‑readable address.

Example code:

import exifread
import re

# Open image as binary
f = open("luotuo.JPG", "rb")
tags = exifread.process_file(f)

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['纬度'] = str(int(match_result[0])) + " " + str(int(match_result[1])) + " " + str(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['经度'] = str(int(match_result[0])) + " " + str(int(match_result[1])) + " " + str(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)

After running the script, the latitude and longitude are displayed, as shown in the accompanying screenshots. The article concludes with suggestions for further exploration, such as using Baidu's API for reverse geocoding and removing metadata for privacy.

pythonGPSTutorialexifimage-metadataexifread
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.