terom@34: # terom@34: # dexif.py - simple EXIF processing for Degal terom@34: # Copyright (C) 2008, Santtu Pajukanta terom@34: # terom@34: # This program is free software; you can redistribute it and/or modify terom@34: # it under the terms of the GNU General Public License as published by terom@34: # the Free Software Foundation; either version 2 of the License, or terom@34: # (at your option) any later version. terom@34: # terom@34: # This program is distributed in the hope that it will be useful, terom@34: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@34: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@34: # GNU General Public License for more details. terom@34: # terom@34: # You should have received a copy of the GNU General Public License terom@34: # along with this program; if not, write to the terom@34: # Free Software Foundation, Inc., terom@34: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@34: # terom@34: terom@34: from subprocess import Popen, PIPE terom@34: terom@34: # TODO This should be user configurable terom@34: EXIFTOOL="/usr/bin/exiftool" terom@34: terom@34: outputTags = [ terom@34: # TODO Create date is in a useless format, needs some strptime love terom@34: ("CreateDate", "Create date"), terom@34: ("Model", "Camera model"), terom@34: ("Aperture", "Aperture"), terom@34: ("ExposureMode", "Exposure mode"), terom@34: ("ExposureCompensation", "Exposure compensation"), terom@34: ("ExposureTime", "Exposure time"), terom@34: ("Flash", "Flash mode"), terom@34: ("ISO", "ISO"), terom@34: ("ShootingMode", "Shooting mode"), terom@34: ("LensType", "Lens type"), terom@34: ("FocalLength", "Focal length") terom@34: ] terom@34: terom@34: terom@34: class ExifError(Exception): terom@34: pass terom@34: terom@34: def parse_exif(filepath): terom@34: """parse_exif(filepath :: String) -> [(String, String)] terom@34: terom@34: Parse EXIF tags from an image file and return them in a dict. terom@34: """ terom@34: terom@34: args = [EXIFTOOL, "-s", "-t", filepath] terom@34: etproc = Popen(args, stdout = PIPE) terom@34: terom@34: output, errors = etproc.communicate() terom@34: terom@34: if etproc.returncode < 0: terom@34: raise ExifError, "exiftool terminated by signal %d" % (-etproc.returnco) terom@34: elif etproc.returncode > 0: terom@34: raise ExifError, "exiftool failed with return code %d" % etproc.returncode terom@34: terom@34: tags = dict(line.split("\t", 1) for line in output.split("\n") if line) terom@34: result = [(descr, tags[key]) for (key, descr) in outputTags if tags.has_key(key)] terom@34: return result