lib/dexif.py
branchuse-distutils
changeset 41 3b1579a7bffb
parent 40 373392025533
child 42 146997912efb
equal deleted inserted replaced
40:373392025533 41:3b1579a7bffb
     1 #
       
     2 # dexif.py - simple EXIF processing for Degal
       
     3 # Copyright (C) 2008, Santtu Pajukanta <santtu@pajukanta.fi>
       
     4 #
       
     5 # This program is free software; you can redistribute it and/or modify
       
     6 # it under the terms of the GNU General Public License as published by
       
     7 # the Free Software Foundation; either version 2 of the License, or
       
     8 # (at your option) any later version.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program; if not, write to the
       
    17 # Free Software Foundation, Inc.,
       
    18 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       
    19 #
       
    20 
       
    21 from subprocess import Popen, PIPE
       
    22 
       
    23 # TODO This should be user configurable
       
    24 EXIFTOOL="/usr/bin/exiftool"
       
    25 
       
    26 outputTags = [
       
    27 # TODO Create date is in a useless format, needs some strptime love
       
    28     ("CreateDate", "Create date"),
       
    29     ("Model", "Camera model"),
       
    30     ("Aperture", "Aperture"),
       
    31     ("ExposureMode", "Exposure mode"),
       
    32     ("ExposureCompensation", "Exposure compensation"),
       
    33     ("ExposureTime", "Exposure time"),
       
    34     ("Flash", "Flash mode"),
       
    35     ("ISO", "ISO"),
       
    36     ("ShootingMode", "Shooting mode"),
       
    37     ("LensType", "Lens type"),
       
    38     ("FocalLength", "Focal length")
       
    39 ]
       
    40 
       
    41 
       
    42 class ExifError(Exception):
       
    43     pass
       
    44 
       
    45 def parse_exif(filepath):
       
    46     """parse_exif(filepath :: String) -> [(String, String)]
       
    47 
       
    48     Parse EXIF tags from an image file and return them in a dict.
       
    49     """
       
    50 
       
    51     args = [EXIFTOOL, "-s", "-t", filepath]
       
    52     etproc = Popen(args, stdout = PIPE)
       
    53 
       
    54     output, errors = etproc.communicate()
       
    55     
       
    56     if etproc.returncode < 0:
       
    57         raise ExifError, "exiftool terminated by signal %d" % (-etproc.returnco)
       
    58     elif etproc.returncode > 0:
       
    59         raise ExifError, "exiftool failed with return code %d" % etproc.returncode
       
    60     
       
    61     tags = dict(line.split("\t", 1) for line in output.split("\n") if line)
       
    62     result = [(descr, tags[key]) for (key, descr) in outputTags if tags.has_key(key)]
       
    63     return result