degal/image.py
changeset 85 7da934333469
parent 77 2a53c5ade434
child 91 17ae33501289
equal deleted inserted replaced
84:891545a38a2b 85:7da934333469
     2     Per-image gallery state
     2     Per-image gallery state
     3 """
     3 """
     4 
     4 
     5 from __future__ import with_statement
     5 from __future__ import with_statement
     6 
     6 
     7 import filesystem, render, html, format
     7 import filesystem, format, thumbnail
     8 from utils import lazy_load
     8 from utils import lazy_load
     9 
     9 
    10 import PIL.Image
    10 import PIL.Image
    11 from lib import EXIF
    11 from lib import EXIF
    12 
    12 
    27         self.next = None
    27         self.next = None
    28 
    28 
    29         # the .html file for this image
    29         # the .html file for this image
    30         self.html = self.parent.subfile(self.basename + '.html')
    30         self.html = self.parent.subfile(self.basename + '.html')
    31 
    31 
       
    32         # our preview/thumbnail
       
    33         self.preview = thumbnail.Thumbnail(self, self.parent.preview_dir, self.config.preview_size)
       
    34         self.thumb = thumbnail.Thumbnail(self, self.parent.thumb_dir, self.config.thumb_size)
       
    35 
    32         # info
    36         # info
    33         self.title = self.name
    37         self.title = self.name
    34         self.description = None
    38         self.description = None
    35 
    39 
    36     @lazy_load
    40     @lazy_load
    37     def stat (self) :
    41     def pil_image (self) :
    38         """
       
    39             Load and return the os.stat info for this file
       
    40         """
       
    41 
       
    42         return super(Image, self).stat()
       
    43     
       
    44     @lazy_load
       
    45     def image (self) :
       
    46         """
    42         """
    47             Loads the image as a PIL.Image
    43             Loads the image as a PIL.Image
    48         """
    44         """
    49         
    45         
    50         # open it up
    46         # open it up
    75         """
    71         """
    76             Load and return the metadata for the image as a dictionary
    72             Load and return the metadata for the image as a dictionary
    77         """
    73         """
    78 
    74 
    79         # load stuff
    75         # load stuff
    80         stat = self.stat
    76         stat = self.stat()
    81         exif = self.exif
    77         exif = self.exif
    82 
    78 
    83         # XXX: avoid having to open the image?
    79         # XXX: avoid having to open the image?
    84         size = self.image.size
    80         size = self.pil_image.size
    85         
    81         
    86         # build
    82         # build
    87         return dict({
    83         return dict({
    88             "File name":        self.name,
    84             "File name":        self.name,
    89             "Resolution":       "%dx%d" % size,
    85             "Resolution":       "%dx%d" % size,
    91             "Last modified":    format.filetime(stat.st_mtime),
    87             "Last modified":    format.filetime(stat.st_mtime),
    92         }, **dict(
    88         }, **dict(
    93             (name, exif[tag]) for tag, name in self.config.exif_tags if exif and tag in exif
    89             (name, exif[tag]) for tag, name in self.config.exif_tags if exif and tag in exif
    94         ))
    90         ))
    95     
    91     
    96     @lazy_load
    92     def stale (self) :
    97     def thumb (self) :
       
    98         """
    93         """
    99             Load and update the thumbnail if needed
    94             Tests if this Image is stale, based on preview/thumb.
   100         """
    95         """
   101 
    96 
   102         # renderer to use
    97         return self.preview.stale() or self.thumb.stale()
   103         # XXX: get from elsewhere
    98     
   104         render_machine = self.config.get_renderer()
    99     def update (self) :
       
   100         """
       
   101             Updates this Image's thumb/preview
       
   102         """
       
   103         
       
   104         if self.preview.stale() :
       
   105             self.preview.update()
   105 
   106 
   106         # render if needed
   107         if self.thumb.stale() :
   107         return render_machine.render_lazy(self,
   108             self.thumb.update()
   108             self.config.thumb_size, self.parent.thumb_dir.subnode(self.name)
       
   109         )
       
   110     
       
   111     @lazy_load
       
   112     def preview (self) :
       
   113         """
       
   114             Load and update the preview if needed
       
   115         """
       
   116 
   109 
   117         # renderer to use
       
   118         # XXX: get from elsewhere
       
   119         render_machine = self.config.get_renderer()
       
   120 
       
   121         return render_machine.render_lazy(self, 
       
   122             self.config.preview_size, self.parent.preview_dir.subnode(self.name)
       
   123         )
       
   124