lib/dexif.py
changeset 34 1fee720888c9
--- /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 <santtu@pajukanta.fi>
+#
+# 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