degal/render.py
author Tero Marttila <terom@fixme.fi>
Wed, 10 Jun 2009 23:33:28 +0300
changeset 84 891545a38a2b
parent 77 2a53c5ade434
permissions -rw-r--r--
change utils.LazyProperty to store the cached value using obj.__dict__
"""
    Rendering images as thumbnails/previews.
"""

import PIL.Image

class RenderMachine (object) :
    """
        RAWR! I'm the render machine!

        TODO: use multithreaded rendering (PIL supports it)
    """

    def __init__ (self, config) :
        """
            Use the given Configuration object's settings for rendering
        """

        self.config = config

    def render_out (self, image, size, file) :
        """
            Creates a thumbnail from the given Image of the given `size`, and saves it at `out`.

            Returns the file for convenience
        """

        # load the PIL.Image
        img = image.image

        # we need to create a copy, as .thumbnail mutates the Image
        img_out = img.copy()

        # then resample to given size
        img_out.thumbnail(size, resample=True)

        # and write out
        img_out.save(file.path)

        return file
   
    def render_lazy (self, image, size, out) :
        """
            Renders the given image with render_out if `out` does not yet exist or is older than image.
        """

        # stat the output file
        out_stat = out.stat(soft=True)

        # compare
        if not out_stat or out_stat.st_mtime < image.stat.st_mtime :
            # render anew
            return self.render_out(image, size, out)

        else :
            # already rendered
            return out