Getting Photo Metadata (EXIF) Using Python

Posted by Mike on March 28th, 2010 filed in Python

Last week, I was trying to find out how to get my photo’s metadata. I had noticed that Windows could display the camera model, creation date and lots of other data on my photos, but I couldn’t remember what that data was called. I finally found what I was looking for. The term is EXIF (Exchangeable Image File Format). For this post, we’ll take a look at the various 3rd party packages that give you access to this information.

My first thought was that the Python Imaging Library would have this functionality, but I hadn’t found the EXIF term yet and couldn’t find that info in PIL’s handbook without it. Fortunately, I did eventually find a way to use PIL via a stackoverflow thread. Here’s the method that it showed:

from PIL import Image
from PIL.ExifTags import TAGS
 
def get_exif(fn):
    ret = {}
    i = Image.open(fn)
    info = i._getexif()
    for tag, value in info.items():
        decoded = TAGS.get(tag, tag)
        ret[decoded] = value
    return ret

This works quite well and returns a nice dictionary object. There are several fields that I found useless, such as the “MakerNote” field which looked like a lot of hexadecimal values, so you’ll probably only want to use certain pieces of data. Here’s a sample of some of the info I got back:

{'YResolution': (180, 1), 
 'ResolutionUnit': 2, 
 'Make': 'Canon', 
 'Flash': 16, 
 'DateTime': '2009:09:11 11:29:10', 
 'MeteringMode': 5, 
 'XResolution': (180, 1), 
 'ColorSpace': 1, 
 'ExifImageWidth': 3264, 
 'DateTimeDigitized': '2009:09:11 11:29:10', 
 'ApertureValue': (116, 32), 
 'FocalPlaneYResolution': (2448000, 169), 
 'CompressedBitsPerPixel': (3, 1), 
 'SensingMethod': 2, 
 'FNumber': (35, 10), 
 'DateTimeOriginal': '2009:09:11 11:29:10', 
 'FocalLength': (26000, 1000), 
 'FocalPlaneXResolution': (3264000, 225), 
 'ExifOffset': 196, 
 'ExifImageHeight': 2448, 
 'ISOSpeedRatings': 100, 
 'Model': 'Canon PowerShot S5 IS', 
 'Orientation': 1, 
 'ExposureTime': (1, 200), 
 'FileSource': '\x03', 
 'MaxApertureValue': (116, 32), 
 'ExifInteroperabilityOffset': 3346, 
 'FlashPixVersion': '0100', 
 'FocalPlaneResolutionUnit': 2, 
 'YCbCrPositioning': 1, 
 'ExifVersion': '0220'}

I don’t really know what all of those values mean, but I know I can use some of them. My purpose for wanting the data is to expand my simple Image Viewer such that it can display more info to the user about their photo.

Here are a few other libraries I found that can supposedly give access to the EXIF data:

I tried the Python Exif Parser and it worked quite well. When I tried to install pyexiv2 on my Python 2.5 box at work, I got an error message about Python 2.6 not being found and then the installer quit. There is no mention on the pyexiv2 website that it requires a certain version of Python to work, so that was a little frustrating. Most of these modules have little or no documentation, which was also pretty frustrating. From what I can tell, EXIF.py is supposed to be used via the command line rather than as an importable module.

Anyway, back to the Python Exif Parser. It’s actually simpler to use than PIL is. Here’s all you need to do after copying the exif.py file into your Python path:

import exif
photo_path = "somePath\to\a\photo.jpg"
data = exif.parse(photo_path)

The code above returns mostly the same information that the PIL snippet does, although it uses integers instead of hex for the “MakersNote” and it has several “Tag0xa406′” fields whereas the PIL data had some numerical fields (which I excluded above). I assume they reference the same information in different ways though.

Anyway, should you find yourself wandering the web when trying to discover this information, hopefully you will stumble upon this post and it will point you in the right direction.

Print Friendly

  • http://www.la-grange.net/karl/ karl

    You can use epinfo as well. It is much faster than anything else I have tried so far. PIL is a bit slow, but I haven’t tried recently.

    epinfo can be found here.
    http://www.lightner.net/lightner/bruce/photopc/epinfo.html

    for example to get the EXIF date, you could do:

    epinfo $file | grep DateTimeOriginal

  • Franz

    Mike,

    pyexiv2’s installation under win32 is a bit tricky, but since it is C-based, it rewards you with a much faster reading performance than EXIF.py. Furthermore, you also get write support for metadata, thus you can store photo captions as IPTC metadata, fix bad timestamps or even geo-tag your pictures.

    For installing pyexiv2 under Python 2.5, please see Robin Mills pages on pyexiv2 (http://clanmills.com/articles/gpsexiftags/). Maybe he can also help out with a Python 2.5 build of the brand new pyexiv 0.2.0

    Franz

  • http://ten.ynottony.net Anthony Batchelor

    For the most part, pyexiv2 rocks.
    It can read AND WRITE exif data to images. This is important if you want to resize an image using PIL and keep the exif data.

    Unfortunately, it chokes on writing some Canon specific tags… There are bug reports about it, but they don’t seem to be making any progress on them… :(

  • http://www.pythonlibrary.org Mike

    @ Everyone,

    My new spam filter was doing too good a job and filtered out a bunch of comments. I apologize for that and will be more careful to check that in the future.

    – Mike

  • http://www.pythonlibrary.org Mike

    @ Karl,

    I didn’t find that EXIF program/module. I’ll have to take a look. Thanks!

    @ fancy,

    I was just showing an example. I would normally use os.path methods but wanted people to be able to follow this article easily. I will be writing about using EXIF data to expand my image viewer program soon.

    @ Franz,

    I thought pyexiv2 looked cool too and I appreciated its documentation. I hope to try it out on my laptop which has Python 2.6 on it.

    @ Anthony,

    I think I found another module that can write EXIF data…I might be thinking of that blogger’s link that I mentioned at the end of the article.

    Thanks for the information, fellows!

    – Mike

  • http://wwwyou.topregistrycleanerreview.net/ Diane

    For this post, we’ll take a look at the various 3rd party packages that give you access to this information.Thanks for the tips G-d bless 😉