Extract Photo Geolocation with Python
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 location information of the photo:
Utilize the PIL library:
from PIL import Image
from PIL.ExifTags import TAGS
def get_geolocation(image_path):
    image = Image.open(image_path)
    exif_data = image._getexif()
    
    if exif_data is not None:
        for tag_id, value in exif_data.items():
            tag_name = TAGS.get(tag_id, tag_id)
            if tag_name == 'GPSInfo':
                return value
    return None
# 传入照片路径作为参数
geolocation = get_geolocation('path/to/photo.jpg')
if geolocation is not None:
    print(geolocation)
else:
    print('No geolocation found.')
Utilize the ExifRead library.
import exifread
def get_geolocation(image_path):
    with open(image_path, 'rb') as image_file:
        tags = exifread.process_file(image_file)
        
        if 'GPS GPSLatitude' in tags and 'GPS GPSLongitude' in tags:
            latitude = tags['GPS GPSLatitude'].values
            longitude = tags['GPS GPSLongitude'].values
            return (latitude, longitude)
    return None
# 传入照片路径作为参数
geolocation = get_geolocation('path/to/photo.jpg')
if geolocation is not None:
    print(geolocation)
else:
    print('No geolocation found.')
These codes will display the geographical location information of the photos. If there is no geographical location information in the photo, it will output ‘No geolocation found.’