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