degal/render.py
changeset 57 8d06e0283b88
parent 51 0f39cb5e4b11
child 77 2a53c5ade434
equal deleted inserted replaced
56:80658a2eebf6 57:8d06e0283b88
     1 """
     1 """
     2     Rendering images as thumbnails/previews.
     2     Rendering images as thumbnails/previews.
     3 """
     3 """
     4 
     4 
     5 import PIL.Image
     5 import PIL.Image
     6 
       
     7 import os.path
       
     8 
     6 
     9 class RenderMachine (object) :
     7 class RenderMachine (object) :
    10     """
     8     """
    11         RAWR! I'm the render machine!
     9         RAWR! I'm the render machine!
    12 
    10 
    18             Use the given Configuration object's settings for rendering
    16             Use the given Configuration object's settings for rendering
    19         """
    17         """
    20 
    18 
    21         self.config = config
    19         self.config = config
    22 
    20 
    23     def render_out (self, img, size, out_path) :
    21     def render_out (self, image, size, file) :
    24         """
    22         """
    25             Creates a thumbnail from the given PIL.Image of the given size, and saves it at out_path.
    23             Creates a thumbnail from the given Image of the given `size`, and saves it at `out`.
       
    24 
       
    25             Returns the file for convenience
    26         """
    26         """
       
    27 
       
    28         # load the PIL.Image
       
    29         img = image.load_image()
    27 
    30 
    28         # we need to create a copy, as .thumbnail mutates the Image
    31         # we need to create a copy, as .thumbnail mutates the Image
    29         img_out = img.copy()
    32         img_out = img.copy()
    30 
    33 
    31         # then resample to given size
    34         # then resample to given size
    32         img_out.thumbnail(size, resample=True)
    35         img_out.thumbnail(size, resample=True)
    33 
    36 
    34         # and write out
    37         # and write out
    35         img_out.save(out_path)
    38         img_out.save(file.path)
    36 
    39 
    37     def render_img (self, img, dirname, filename) :
    40         return file
       
    41    
       
    42     def render_lazy (self, image, size, out) :
    38         """
    43         """
    39             Renders new thumbnails/previews from the given loaded image and filename based on the current configuration.
    44             Renders the given image with render_out if `out` does not yet exist or is older than image.
    40             
       
    41             Note: this does not check for the existance of the thumbs/previews subdirs!
       
    42         """
    45         """
    43 
    46 
    44         # once for each size
    47         # stat the output file
    45         for subdir, size in (
    48         out_stat = out.stat(soft=True)
    46             (self.config.thumb_dir, self.config.thumb_size),
       
    47             (self.config.preview_dir, self.config.preview_size)
       
    48         ) :
       
    49             # resize and write out
       
    50             self.render_out(img, size, os.path.join(dirname, subdir, filename)
       
    51 
    49 
       
    50         # compare
       
    51         if not out_stat or out_stat.st_mtime < image.load_stat().st_mtime :
       
    52             # render anew
       
    53             return self.render_out(image, size, file)
       
    54 
       
    55         else :
       
    56             # already rendered
       
    57             return file