lib/image.py
changeset 28 70b6c13d084f
parent 27 301d738b1181
child 33 4943047bfcb5
equal deleted inserted replaced
27:301d738b1181 28:70b6c13d084f
    20 
    20 
    21 import os, os.path
    21 import os, os.path
    22 
    22 
    23 import PIL.Image
    23 import PIL.Image
    24 
    24 
    25 import settings, utils
    25 import settings, utils, log
    26 from log import index, render
       
    27 from template import image as image_tpl
    26 from template import image as image_tpl
    28     
    27     
    29 class Image (object) :
    28 class Image (object) :
    30     def __init__ (self, dir, name) :
    29     def __init__ (self, dir, name) :
    31         # the image filename, e.g. DSC3948.JPG
    30         # the image filename, e.g. DSC3948.JPG
   102 
   101 
   103         self.img_size = img.size
   102         self.img_size = img.size
   104 
   103 
   105         for out_path, geom in ((self.thumb_path, settings.THUMB_GEOM), (self.preview_path, settings.PREVIEW_GEOM)) :
   104         for out_path, geom in ((self.thumb_path, settings.THUMB_GEOM), (self.preview_path, settings.PREVIEW_GEOM)) :
   106             # if it doesn't exist, or it's older than the image itself, generate
   105             # if it doesn't exist, or it's older than the image itself, generate
   107             if not (os.path.exists(out_path) and os.stat(out_path).st_mtime > self.timestamp) :
   106             if utils.mtime(out_path) < self.timestamp :
   108                 render.info("Create thumbnailed image at %s with geom %s", out_path, geom)
   107                 log.info("render [%sx%s]", geom[0], geom[1], wait=True)
   109                 
   108                 
   110                 # XXX: is this the most efficient way to do this? It seems slow
   109                 # XXX: is this the most efficient way to do this? It seems slow
   111                 out_img = img.copy()
   110                 out_img = img.copy()
   112                 out_img.thumbnail(geom, resample=True)
   111                 out_img.thumbnail(geom, resample=True)
   113                 out_img.save(out_path)
   112                 out_img.save(out_path)
       
   113 
       
   114                 log.done()
   114         
   115         
   115         # look for the metadata file
   116         # look for the metadata file
   116         title_path = self.dir.pathFor(self.base_name + '.txt')
   117         title_path = self.dir.pathFor(self.base_name + '.txt')
   117         
   118         
   118         self.title, self.descr = utils.readTitleDescr(title_path)
   119         self.title, self.descr = utils.readTitleDescr(title_path)
   119         
   120         
   120         if not self.title :
   121         if not self.title :
   121             self.title = self.name
   122             self.title = self.name
       
   123         
       
   124         if utils.mtime(self.html_path) < self.timestamp :
       
   125             log.info("render %s.html", self.name)
   122 
   126 
   123         render.info("Rendering image %s", self.path)
   127             image_tpl.render_to(self.html_path,
   124 
   128                 stylesheet_url             = self.dir.inRoot('style.css'),
   125         image_tpl.render_to(self.html_path,
   129                 title                      = self.title,
   126             stylesheet_url             = self.dir.inRoot('style.css'),
   130                 breadcrumb                 = self.breadcrumb(),
   127             title                      = self.title,
   131                 
   128             breadcrumb                 = self.breadcrumb(),
   132                 prev                       = self.prev,
   129             
   133                 next                       = self.next,
   130             prev                       = self.prev,
   134                 img                        = self,
   131             next                       = self.next,
   135                 
   132             img                        = self,
   136                 description                = self.descr,
   133             
   137                 
   134             description                = self.descr,
   138                 filename                   = self.name,
   135             
   139                 img_size                   = self.img_size,
   136             filename                   = self.name,
   140                 file_size                  = self.filesize,
   137             img_size                   = self.img_size,
   141                 timestamp                  = self.timestamp,
   138             file_size                  = self.filesize,
   142                 
   139             timestamp                  = self.timestamp,
   143                 shorturl                   = self.dir.inRoot('s', self.shorturl_code),
   140             
   144                 shorturl_code              = self.shorturl_code,
   141             shorturl                   = self.dir.inRoot('s', self.shorturl_code),
   145                 
   142             shorturl_code              = self.shorturl_code,
   146                 series_url                 = self.dir.inRoot('series/%s/%s' % (self.series_act, self.shorturl_code)),
   143             
   147                 series_verb                = self.series_verb,
   144             series_url                 = self.dir.inRoot('series/%s/%s' % (self.series_act, self.shorturl_code)),
   148             )   
   145             series_verb                = self.series_verb,
       
   146         )   
       
   147     
   149     
   148     def __str__ (self) :
   150     def __str__ (self) :
   149         return "Image `%s' in `%s'" % (self.name, self.dir.path)
   151         return "Image `%s' in `%s'" % (self.name, self.dir.path)
       
   152