Python Friday #27: Reading Exif Metadata From Images

While sorting my images I came up with a little idea: Can I find all images I took with a specific lens? As it turns out, Python offers a wide range of packages to get this information from the Exif metadata.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

What is Exif?

Exchangeable image file format (officially Exif, according to JEIDA/JEITA/CIPA specifications) is a standard that specifies the formats for images, sound, and ancillary tags used by digital cameras (including smartphones), scanners and other systems handling image and sound files recorded by digital cameras – source: Wikipedia

 

Python libraries

If you search for ‘Exif’ in the Python Package Index PyPI.org you get more than 160 results. Some packages are hopelessly outdated, others have a specific use-case in mind that does not work for me. I tried 3 packages but quickly abandoned two of them.

I finally settled with exif because I could write my code in a way that made sense for me and it is actively developed (the newest release was 2 days ago). I suggest you start with this package and see how far it brings you. You can install it with this command in your virtual environment:

 

Reading Exif metadata

To read the make and model of the camera and the lens you need these few lines of code:

This code opens your image file in the binary mode and the with block helps you with the exception handling (as described here). I run this function with an image from my iPhone and got this result:

The exif package supports a wide range of metadata attributes. You can get a list of them with the dir() function:

 

Conclusion

The exif package gives me all I need to know about my images. It has an understandable syntax and a simple abstraction over the Exif metadata section.

Leave a Comment

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