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