#!/usr/bin/env python """pyexiftool.py A small wrapper around the perl 'exiftool' command. This library has an understand of ExifTag 'types', and has some intelligence dealing with MakerNote tags. It was written in haste by jason moiron (email: 'am1vaXJvbkBqbW9pcm9uLm5ldA==\n'.decode('base64')) in order to have some exif support for an online gallery. EXIF.py is monolithic and outdated, and there haven't been many attempts at creating an EXIF library that works with many makernote tags. There was a LONG attempt at making something that could snarf out perl's "exiftool" tables, but the amount of effort that went into that is probably better put towards making the actual gallery. """ import subprocess, os PIPE = subprocess.PIPE STDOUT = subprocess.STDOUT def which(program): """Similar to command line tool "which" """ for path in os.environ['PATH'].split(os.pathsep): if not path.endswith('/'): path += '/' if os.path.exists(path + program): return path + program return None errormsg = """\ Error: exiftool not found (apt-get install libimage-exiftool-perl)""" exiftool = which('exiftool') if not exiftool: raise Exception(errormsg) class ExifTag: def __init__(self, line): split = [sp.strip() for sp in line.split(':')] self.name = split[0] if len(split) == 1: self.value = '' else: self.value = ':'.join(split[1:]) def __str__(self): return str(self.value) class ExifData: def __init__(self, filename): if not os.path.exists(filename): raise IOError("No such file: '%s'" % filename) self.maxTagLen = 0 self.filename = filename self.cmd = [exiftool, filename] self.output = self.run() self.generate_cache() def run(self): process = subprocess.Popen(self.cmd, stdout=PIPE, stderr=STDOUT) process.wait() # it is custom to return 0 on success return process.stdout.readlines() def generate_cache(self): self.output = [line.strip() for line in self.output] self._cache = {} for line in self.output: tag = ExifTag(line) self._cache[tag.name] = tag if len(tag.name) > self.maxTagLen: self.maxTagLen = len(tag.name) def dump(self): for tag, value in self._cache.items(): print tag.ljust(self.maxTagLen) + ': ' + str(value) if __name__ == '__main__': import sys, optparse parser = optparse.OptionParser() (options, args) = parser.parse_args() if not len(args): parser.print_usage() filename = args[0] ed = ExifData(filename) ed.dump()