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