Extracting image EXIF data with Python
Most digital cameras and smartphones embed EXIF (EXchangeable Image Format) data into the photographs they capture. This can include: camera make & model, date and time, camera settings like orientation, aperture, ISO, shutter speed, focal, length and even GPS location.
After a bit of experimentation I have found the following method of using the undocumented ExifTags module in the Python Image Library (PIL) to be the simplest way to extract EXIF tags from images using Python. There are other EXIF modules available for Python however currently PIL is the simplest to install on Mac OS X.
from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_data(fname):
"""Get embedded EXIF data from image file."""
ret = {}
try:
img = Image.open(fname)
if hasattr( img, '_getexif' ):
exifinfo = img._getexif()
if exifinfo != None:
for tag, value in exifinfo.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except IOError:
print 'IOERROR ' + fname
return ret
The above code was based on the code snippet in Paolo’s answer to this StackOverflow question. I have added basic exception handling and a check for the existence of the _getexif attribute prior to accessing it.
You can leave a response, or trackback from your own site.







It only extracts very basic Exif. If you need to check what’s in the file as a whole, try my software FileMind (a free beta is available under http://bit.ly/FileMindBeta). It not only shows you the entire Exif (plus maker notes), but also XMP and Iptc/IIM, as a reference.
Ugh, ignore the comment above. It’s some proprietary Windows-only nonsense — not Python, and certainly not Open Source.
Nice quick article though…
Leon,
not Open Source, not Mac native. But currently in beta and free (as in free beer…) – and who doesn’t have Parallals these days? And it’s still helpful for drilling down what really happens to your values.
Thanks Daniel! Used it to get the “Date Taken” (DateTimeOriginal) attribute from my pics so I can auto-rename them with it
.