DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB

Trending

  • A Practical Blueprint for Deploying Agentic Solutions
  • How to Interpret the Number of Spring ApplicationContexts in Integration Tests
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • Mocking Kafka for Local Spring Development
  1. DZone
  2. Data Engineering
  3. Data
  4. Getting GPS EXIF Data With Python

Getting GPS EXIF Data With Python

Let's pull some data.

By 
Mike Driscoll user avatar
Mike Driscoll
·
Jan. 24, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
14.9K Views

Join the DZone community and get the full member experience.

Join For Free

Did you know that you can get EXIF data from JPG image files using the Python programming language? You can use Pillow, the Python Imaging Library's friendly fork to do so. You can read an article about that on this website if you want to.

Here is some example code for getting regular EXIF data from a JPG file:

Python
 




x
19


 
1
# exif_getter.py
2
 
           
3
from PIL import Image
4
from PIL.ExifTags import TAGS
5
 
           
6
 
           
7
def get_exif(image_file_path):
8
    exif_table = {}
9
    image = Image.open(image_file_path)
10
    info = image.getexif()
11
    for tag, value in info.items():
12
        decoded = TAGS.get(tag, tag)
13
        exif_table[decoded] = value
14
    return exif_table
15
 
           
16
 
           
17
if __name__ == "__main__":
18
    exif = get_exif("bridge.JPG")
19
    print(exif)



This code was run using the following image:

In this article, you will focus on how to extract GPS tags from an image. These are special EXIF tags that are only present if the camera that took the photo had its location information turned on for the camera. You can also add GPS tags on your computer after the fact.

For example, I added GPS tags to this photo of Jester Park, which is in Granger, IA:

To get access to those tags, you'll need to take the earlier code example and do some minor adjustments:

Python
 




xxxxxxxxxx
1
25


 
1
# gps_exif_getter.py
2
 
           
3
from PIL import Image
4
from PIL.ExifTags import TAGS, GPSTAGS
5
 
           
6
 
           
7
def get_exif(image_file_path):
8
    exif_table = {}
9
    image = Image.open(image_file_path)
10
    info = image.getexif()
11
    for tag, value in info.items():
12
        decoded = TAGS.get(tag, tag)
13
        exif_table[decoded] = value
14
 
           
15
    gps_info = {}
16
    for key in exif_table['GPSInfo'].keys():
17
        decode = GPSTAGS.get(key,key)
18
        gps_info[decode] = exif_table['GPSInfo'][key]
19
 
           
20
    return gps_info
21
 
           
22
 
           
23
if __name__ == "__main__":
24
    exif = get_exif("jester.jpg")
25
    print(exif)



To get access to the GPS tags, you need to import GPSTAGS from PIL.ExifTags. Then after parsing the regular tags from the file, you add a second loop to look for the "GPSInfo" tag. If that's present, then you have GPS tags that you can extract.

When you run this code, you should see the following output:

JSON
 




xxxxxxxxxx
1


 
1
{'GPSLatitudeRef': 'N',
2
 'GPSLatitude': (41.0, 47.0, 2.17),
3
 'GPSLongitudeRef': 'W',
4
 'GPSLongitude': (93.0, 46.0, 42.09)}



You can take this information and use it to load a Google map with Python or work with one of the popular GIS-related Python libraries.

Python (language) Exif Data (computing)

Published at DZone with permission of Mike Driscoll. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook