How to Extract GPS Coordinates from a Photo: The USAID Mystery

When we take photos with our phones or digital cameras, the images may contain more information than we can see. For example, if GPS was enabled when the photo was taken, the image may contain latitude and longitude coordinates. That is, we can use this information to find out where the photo was taken.

In this post, you will learn how to extract GPS data from an image using Python. We will look at how to get the coordinates from the image’s EXIF metadata and then plot the location on a map.

For this example, I will use a photo of a USAID nutrition pack. Once we have extracted the GPS coordinates, we will see where the photo was taken. I will leave it up to you to decide whether the pack should be there.

How to Extract GPS Coordinates in Python and Plot Them on a Map

Now that we know that images can contain GPS information, we will look at how to extract this information using Python. We will do this in four steps. First, we will install the libraries we need. Next, we will read the EXIF metadata from the image and extract the GPS coordinates. Finally, we will use the coordinates to show where the photo was taken on a map.

Step 1: Setting Up Your Python Environment

Before we can extract the GPS coordinates, we need to install a few Python libraries. In this example, we will use:

  • Pillow: To open and work with the image.
  • ExifRead: To read the EXIF metadata stored in the image.
  • Folium: To plot the GPS coordinates on an interactive map.

We can install these libraries using pip:

pip install Pillow ExifRead foliumCode language: Bash (bash)

Now that we have installed the libraries, we can start reading the EXIF metadata from our image.

Step 2: Extracting EXIF Data from the Photo

As mentioned earlier, photos can contain EXIF metadata with information about when the photo was taken, camera settings, and sometimes GPS coordinates. If the image contains GPS information, we can use it to find the latitude and longitude recorded in the photo.

Here is how we can read the EXIF data using Python:

import exifread

# Open the image file
with open('nutrition_pack.jpg', 'rb') as f:
    tags = exifread.process_file(f)

# Check the tags available
for tag in tags:
    print(tag, tags[tag])Code language: PHP (php)

In the code above, we first open the image file (nutrition_pack.jpg) in binary mode. We then use exifread.process_file() to read the EXIF metadata. Finally, we loop through the extracted tags and print their names and values. That is, we can see what information is stored in the image, including any GPS data.

Step 3: Extracting the GPS Coordinates

Now that we have read the EXIF metadata, we can look for the GPS coordinates. If the image contains location information, we can find the latitude and longitude in the GPS GPSLatitude and GPS GPSLongitude tags.

Here is how we can extract these values using Python:

# Helper function to convert a list of Ratio to float degrees
def dms_to_dd(dms):
    degrees = float(dms[0])
    minutes = float(dms[1])
    seconds = float(dms[2])
    return degrees + (minutes / 60.0) + (seconds / 3600.0)
# Updated keys to match your EXIF tag names
lat_key = 'GPS GPSLatitude'
lat_ref_key = 'GPS GPSLatitudeRef'
lon_key = 'GPS GPSLongitude'
lon_ref_key = 'GPS GPSLongitudeRef'

# Check if GPS data exists
if lat_key in tags and lon_key in tags and lat_ref_key in tags and lon_ref_key in tags:
    # Extract raw DMS data
    lat_values = tags[lat_key].values
    lon_values = tags[lon_key].values

    # Convert to decimal degrees
    latitude = dms_to_dd(lat_values)
    longitude = dms_to_dd(lon_values)

    # Adjust for hemisphere
    if tags[lat_ref_key].printable != 'N':
        latitude = -latitude
    if tags[lon_ref_key].printable != 'E':
        longitude = -longitude

    print(f"GPS Coordinates: Latitude = {latitude}, Longitude = {longitude}")
else:
    print("No GPS data found!")Code language: Python (python)

In the code above, we first check whether the image contains the four GPS tags we need: latitude, longitude, and their directional references. If all four tags are present, we extract the coordinates and convert them from degrees, minutes, and seconds (DMS) to decimal degrees.

We also use the directional references to determine whether the coordinates should be positive or negative. For example, latitudes south of the equator and longitudes west of the prime meridian are negative. Finally, we print the coordinates. If any of the required GPS tags are missing, we print a message instead.

Step 4: Plotting the Location on a Map

Now for the fun part! We have extracted the GPS coordinates from our image, and we can use them to see where the photo was taken. For this, we will use the folium library in Python to create an interactive map and add a marker at the location indicated by the GPS data.

Here is how we can plot the coordinates on a map:

import folium

# Create a map centered around the coordinates
map_location = folium.Map(location=[latitude, longitude], zoom_start=12)

# Add a marker for the photo location
folium.Marker([latitude, longitude], popup="Photo Location").add_to(map_location)

# Save map to HTML
map_location.save('map_location.html')Code language: Python (python)

In the code above, we use folium to create a map centered on the GPS coordinates we extracted from the image. We then add a marker with a popup that says “Photo Location”. Finally, we save the map as an HTML file that we can open in a web browser. That is, we can now see the location recorded in the image’s GPS metadata on an interactive map.

Where Was This Photo Taken?

We have now extracted the GPS coordinates from our photo of the USAID nutrition pack and plotted them on a map. We can see where the photo was reportedly taken, but there is another question I want you to consider:

map of the word with the coordinates of the USAID pack.

Should the USAID Nutrition Pack Be in This Location?

Now that we have plotted the GPS coordinates on a map, I want you to take a closer look at the location. Does it make sense for a USAID nutrition pack to be there?

USAID has provided humanitarian assistance in different parts of the world, including food and nutritional support. However, looking at the location recorded in this photo raises an interesting question: Is this where you would expect to find such a package?

Of course, the GPS coordinates alone cannot tell us how the package got there or why. Still, I think it is interesting to see where the photo was reportedly taken.

Take a look at the map and let me know what you think. Is this a location where you would expect to find a USAID nutrition pack?

Conclusion: Where Was the Photo Taken?

In this post, we have looked at how to extract GPS coordinates from an image using Python. We started by reading the EXIF metadata, extracted the latitude and longitude, and then used folium to plot the coordinates on an interactive map.

For our example, I used a photo of a USAID nutrition pack. We now know what location is recorded in the image’s metadata, but that leaves us with another question: Why was the package photographed there?

I will leave that question for you to consider. If you have any thoughts about the location, or questions about extracting GPS data from images using Python, feel free to leave a comment below.

I hope you found this post useful. If you did, please share it with others who might be interested in working with EXIF data in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top