degal/image.py
changeset 120 55cb7fc9c8fb
parent 118 60b126ff0b74
child 122 292aaba6d6ec
equal deleted inserted replaced
119:e7855eefb4c7 120:55cb7fc9c8fb
     1 """
     1 """
     2     Per-image gallery state
     2     Per-image gallery state
     3 """
     3 """
     4 
     4 
     5 from __future__ import with_statement
     5 import filesystem, format, thumbnail, exif
     6 
       
     7 import filesystem, format, thumbnail
       
     8 from utils import lazy_load
     6 from utils import lazy_load
     9 
     7 
    10 import PIL.Image
     8 import PIL.Image
    11 from lib import EXIF
       
    12 
     9 
    13 class Image (filesystem.File) :
    10 class Image (filesystem.File) :
    14     """
    11     """
    15         An Image is a filesystem File that represents an image that can be thumbnailed, and handled.
    12         An Image is a filesystem File that represents an image that can be thumbnailed, and handled.
    16     """
    13     """
    55         return self.img.size
    52         return self.img.size
    56 
    53 
    57     @lazy_load
    54     @lazy_load
    58     def exif (self) :
    55     def exif (self) :
    59         """
    56         """
    60             Loads the EXIF data for the image and returns as a dict of EXIF tag names -> values.
    57             Loads the ExifHandler object for this image.
    61 
    58 
    62             Returns None if no exif data was found
    59             Returns None if no exif data was found
    63         """
    60         """
    64 
    61 
    65         # load
    62         # look up using exif
    66         with self.open('rb') as fh :
    63         return exif.load(self.config, self)
    67             # XXX: details=False?
       
    68             exif = EXIF.process_file(fh)
       
    69             
       
    70         # empty dict -> no exif data
       
    71         if exif :
       
    72             return exif
       
    73 
       
    74         else :
       
    75             return None
       
    76     
    64     
    77     @lazy_load
    65     @lazy_load
    78     def metadata (self) :
    66     def metadata (self) :
    79         """
    67         """
    80             Load and return the metadata for the image as a dictionary
    68             Load and return the metadata for the image as a dictionary
    85 
    73 
    86         # XXX: avoid having to open the image?
    74         # XXX: avoid having to open the image?
    87         size = self.img_size
    75         size = self.img_size
    88         
    76         
    89         # build
    77         # build
    90         metadata = dict({
    78         metadata = [
    91             "File name":        self.name,
    79             ("File name",       self.name),
    92             "Resolution":       "%dx%d" % size,
    80             ("Resolution",      "%dx%d" % size),
    93             "File size":        format.filesize(stat.st_size),
    81             ("File size",       format.filesize(stat.st_size)),
    94             "Last modified":    format.filetime(stat.st_mtime),
    82             ("Last modified",   format.filetime(stat.st_mtime)),
    95         })
    83         ]
    96         
    84         
    97         # optionally load Exif metadata
    85         # optionally load Exif metadata
    98         if self.config.with_exif :
    86         if self.config.with_exif and self.exif :
    99             exif = self.exif
    87             exif_tag_name = dict(self.config.exif_tags)
       
    88             exif_tags = (tag for tag, name in self.config.exif_tags)
   100 
    89 
   101             # Get the wanted tags
    90             # merge the wanted tags
   102             metadata.update(dict(
    91             metadata.extend((
   103                 (name, exif[tag]) for tag, name in self.config.exif_tags if exif and tag in exif
    92                 (exif_tag_name[tag], value) for tag, value in self.exif.image_tags(exif_tags)
   104             ))
    93             ))
   105 
    94 
   106         return metadata
    95         return metadata
   107     
    96     
   108     def stale (self) :
    97     def stale (self) :