How to Get Image EXIF Data with Python
Digital photographs of the JPG filetype include a type of metadata that is known as Exchangeable Image File Format or simply Exif. You can access these tags using the PIL.ExifTags module.
You can learn more about this module by checking out its documentation.
In this article, you will focus on getting this image metadata using Pillow.
Getting Exif Tag Data
The Exchangeable Image File Format contains quite a bit of data about your photographs. It includes your camera’s settings when you took the image, such as ISO, aperture, focal length, exposure value, and more. Some cameras also include geographic coordinates in their Exif tags.
You will be using one of the author’s photos of a local bridge from the state of Iowa as it contains good Exif data:
Pillow can be used to retrieve the Exif metadata from the bridge photo above. To see how this works, create a file named exif_getter.py and add this code to it:
In this example, you create get_exif(), which takes in the path to the image that you want to extract Exif tags from. Then you create an exif_table, which is a Python dictionary. Next, you open up the image using Pillow and extract the Exif information using _getexif(). After that, you loop over the items you extracted and use Pillow’s TAGS, which is a hard-coded Python dictionary that maps a hexadecimal value to a human-readable name.
Once you have decoded the Exif data and populated the exif_table dictionary, you return the data. When you run this code using the bridge image, you will get a lot of output.
The following is a sample of some of the data that is returned:
Some of the data that is returned is not human-readable. One of the shortest examples is ComponentsConfiguration mentioned above. But there are several others. The ComponentsConfiguration property is a representation of the color planes in the image, starting at the 1st plane and going through the 4th plane. Not all images have the same amount of data as others. It depends on your camera and how much data it can record.
If you have edited the image yourself, you may have inadvertently deleted the Exif data. While it’s nice to crop or enhance a photo, you need to keep that in mind if you want to preserve the Exif data. You can use it to help you take photos in the future since it has all your camera settings at the time the shot was taken.
Now let’s find out how to get the Global Positioning System (GPS) Exif information from your images!
Getting GPS Exif Data
Camera phones can tag your photos with location data when you take a photo. Most phones have this off by default, but you can turn it on if you want to. Some newer cameras have built-in Global Positioning System (GPS) that provides this capability. The digital single-lens reflex (DSLR) cameras usually have some kind of accessory you can attach to your camera to have it record GPS too. Or you can add the GPS tags yourself on your computer!
Regardless of how the GPS information ends up in your photo, you can use Pillow to get it. For this example, you will use this image that was taken at Jester Park in Granger, Iowa:
Now that you have a photo to use, you need to write some code! Open up your editor and create a new file named gps_exif_getter.py. Then add the following:
The first new item here is that you need to import GPSTAGS in addition to TAGS. The next thing you need to do is add a second loop (lines 16-18) after decoding the regular Exif tags. Right before that second loop, you create a new gps_info dictionary. Then you loop over the keys in the exif_table['GPSInfo'] dictionary and decode the GPS tags in the same way that you did with the regular Exif tags.
When you run this code, you should get the following output:
You can use this information to look up the image's location on a map. You could even write some code for Google Maps that you could use to programmatically load up a map of where you took your photos, if you wanted to.
Wrapping Up
Image metadata is important. You can use it to learn how to take better photographs. If your camera supports geographic tagging, you can use that data to programmatically determine where a photo was taken.
You can use EXIF data to build all kinds of fun applications that you can use to analyze photos, label them, combine them, and do much more using Python or other external tools.
Want to Learn More?
You can learn more about what you can do with Python and Pillow in Mike’s book, Pillow: Image Processing with Python







