The ExifTags module exposes several enum.IntEnum classes which provide constants and clear-text names for various well-known EXIF tags.
Use the folium ibrary to create a map and add a marker at the extracted coordinates
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
def get_exif_data(image):
exif_data = {}
info = image._getexif()
if info:
for tag, value in info.items():
tag_name = TAGS.get(tag, tag)
exif_data[tag_name] = value
if tag_name == "GPSInfo":
gps_data = {}
for t in value:
sub_tag = GPSTAGS.get(t, t)
gps_data[sub_tag] = value[t]
exif_data["GPSInfo"] = gps_data
return exif_data
def get_lat_lon(exif_data):
if "GPSInfo" in exif_data:
gps_info = exif_data["GPSInfo"]
print(gps_info)
gps_latitude = gps_info.get("GPSLatitude")
gps_latitude_ref = gps_info.get("GPSLatitudeRef")
gps_longitude = gps_info.get("GPSLongitude")
gps_longitude_ref = gps_info.get("GPSLongitudeRef")
if gps_latitude and gps_latitude_ref and gps_longitude and gps_longitude_ref:
lat = convert_to_decimal_degrees(gps_latitude)
if gps_latitude_ref != "N":
lat = -lat
lon = convert_to_decimal_degrees(gps_longitude)
if gps_longitude_ref != "E":
lon = -lon
return lat, lon
return None, None
def convert_to_decimal_degrees(gps_tuple):
degrees, minutes, seconds = gps_tuple
decimal_degrees = degrees + (minutes / 60.0) + (seconds / 3600.0)
return decimal_degrees
import folium
# Use the folium ibrary to create a map and add a marker at the extracted coordinates
def create_a_map(lat, lon):
# Create a map centered around the extracted coordinates
map = folium.Map(location=[lat, lon], zoom_start=17)
# Add a marker at the location
folium.Marker([lat, lon], popup='Image Location').add_to(map)
# Save the map to an HTML file
map.save('map.htm')
image_path = "source.jpg"
image = Image.open(image_path)
exif_data = get_exif_data(image)
lat, lon = get_lat_lon(exif_data)
if lat and lon:
print(f"Latitude: {lat}, Longitude: {lon}")
create_a_map(lat, lon)
else:
print("No GPS data found.")