degal/exif.py
author Tero Marttila <terom@fixme.fi>
Thu, 02 Jul 2009 23:59:58 +0300
changeset 146 c226063eeb65
parent 130 94888270dae0
permissions -rw-r--r--
remove obsolete db.py
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    The various Exif backends that we have
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
from __future__ import with_statement
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import utils
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
122
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
     9
MIRROR_NONE         = 0
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    10
MIRROR_HORIZONTAL   = 1
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    11
MIRROR_VERTICAL     = 2
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    12
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    13
ROTATE_NONE         = 0
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    14
ROTATE_90           = 90
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    15
ROTATE_180          = 180
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    16
ROTATE_270          = 270
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    17
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
class ExifHandler (object) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
        Our generic interface for Exif data
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    def __init__ (self, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
            Load Exif data from the given File.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        self.file = file
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    
122
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    30
    def image_tag_value (self, tag, default=None) :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    31
        """
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    32
            Load and return the raw binary value for the given main image tag.
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    33
        """
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    34
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    35
        return default
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    36
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    def image_tag (self, tag, default=None) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
            Load and return the human-readable unicode-safe value for the given main image tag.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
122
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    42
        return default
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    def image_tags (self, tags) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
            Load and return the given tags for the main image as a sequence of (tag, value) tuples , following the same
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
            rules as image_tag, except there should mainly be only one call to this function, encompassing all the
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
            relevant tags.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
            The returned list should omit those values which were not found.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        for tag in tags :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
            # try and fetch value
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
            value = self.image_tag(tag)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
            # filter out default not-found
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
            if value is not None :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
                yield tag, value
122
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    60
    
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    61
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    62
    def get_orientation (self) :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    63
        """
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    64
            Get the orientation of the image in terms of mirroring and rotation in integer degrees clockwise.
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    65
            
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    66
            Returns a (MIRROR_*, ROTATE_*) tuple, or None if the Orientation tag could not be found.
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    67
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    68
            Returns None if it could not be found.
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    69
        """
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    70
        
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    71
        # load value
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    72
        orientation = self.image_tag_value('Orientation')
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    73
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    74
        # map to tuple or None
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    75
        # XXX: these are from EXIFpy, verify
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    76
        return {
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    77
            1:  (MIRROR_NONE,       ROTATE_NONE ),  # Horizontal (normal)
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    78
            2:  (MIRROR_HORIZONTAL, ROTATE_NONE ),  # Mirrored horizontal
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    79
            3:  (MIRROR_NONE,       ROTATE_180  ),  # Rotated 180
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    80
            4:  (MIRROR_VERTICAL,   ROTATE_NONE ),  # Mirrored vertical
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    81
            5:  (MIRROR_HORIZONTAL, ROTATE_270  ),  # Mirrored horizontal then rotated 90 CCW
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    82
            6:  (MIRROR_NONE,       ROTATE_90   ),  # Rotated 90 CW
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    83
            7:  (MIRROR_HORIZONTAL, ROTATE_90   ),  # Mirrored horizontal then rotated 90 CW
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    84
            8:  (MIRROR_NONE,       ROTATE_270  ),  # Rotated 90 CCW
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    85
        }.get(orientation, None)
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
try :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    import pyexiv2
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
except ImportError :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    PyExiv2_Handler = None
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    class PyExiv2_Handler (ExifHandler) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        NAME = 'pyexiv2'
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        def __init__ (self, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
                Load as pyexiv2.Image
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
            
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
            # create
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
            self.image = pyexiv2.Image(file.path)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
            # load
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
            self.image.readMetadata()
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
        
122
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   108
        def image_tag_value (self, tag, default=None) :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   109
            # try with likely prefixes
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   110
            for prefix in ('Exif.Photo', 'Exif.Image') :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   111
                try :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   112
                    return self.image["%s.%s" % (prefix, tag)]
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   113
                
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   114
                except (IndexError, KeyError) :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   115
                    # nope...
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   116
                    continue
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   117
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   118
            else :
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   119
                # not ofund
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   120
                return default
292aaba6d6ec auto-orientation, although no mirroring support yet
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   121
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
        def image_tag (self, tag, default=None) :
121
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   123
            # try with likely prefixes
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   124
            for prefix in ('Exif.Photo', 'Exif.Image') :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   125
                try :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   126
                    return self.image.interpretedExifValue("%s.%s" % (prefix, tag))
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   127
                
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   128
                except (IndexError, KeyError) :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   129
                    # nope...
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   130
                    continue
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   131
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   132
            else :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   133
                # not ofund
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
                return default
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
try :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    from lib import EXIF
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
except ImportError :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
    EXIF_Handler = None
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
    class EXIF_Handler (ExifHandler) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        NAME = 'EXIFpy'
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
        def __init__ (self, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
                Load using EXIF.process_file
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
            
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
            with file.open('rb') as fh :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
                self.tags = EXIF.process_file(fh)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
        
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
        def image_tag (self, tag, default=None) :
121
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   155
            # try with likely prefixes
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   156
            for prefix in ('Image', 'EXIF') :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   157
                name = prefix + ' ' + tag
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   158
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   159
                if name in self.tags :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   160
                    return self.tags[name]
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   161
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   162
            else :
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   163
                # not found
3bed35e79f41 fix pyexif2/EXIFpy handlers to try different prefixes
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   164
                return default
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
# ExifHandler implementations to use, in preference order
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
EXIF_HANDLERS = [
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    # reasonably fast, but requires the native library and extension module
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
    PyExiv2_Handler,
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
    # pure-python, but very, very slow
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
    EXIF_Handler,
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
]
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
# default ExifHandler to use
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
EXIF_HANDLER = utils.first(EXIF_HANDLERS)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
# ExifHandler implementations by name
130
94888270dae0 fix exif.py to load correctly with incomplete exif handlers
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
   179
EXIF_HANDLERS_BY_NAME = dict((h.NAME, h) for h in EXIF_HANDLERS if h)
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   180
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
def load (config, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
        Load Exif data using the configured Exif handler, and return the instance.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
        Returns None if there wasn't any Exif handler available for use.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
    if config.exif_handler :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
        # explicit
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
        exif_handler = config.exif_handler
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
    elif config.exif_handler_name :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
        # by name
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
        exif_handler = EXIF_HANDLERS_BY_NAME[config.exif_handler_name]
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
    else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
        # default
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
        exif_handler = EXIF_HANDLER
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   199
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
    if exif_handler :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   201
        # found one, use it
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
        return exif_handler(file)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
    else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   205
        # nothing :(
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
        return None
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207