degal/thumbnail.py
author Tero Marttila <terom@fixme.fi>
Thu, 11 Jun 2009 21:50:48 +0300
changeset 95 3b00bd676fc9
parent 85 7da934333469
child 115 d5aa320697df
permissions -rw-r--r--
fix Thumbnail.stale/File.older_than behaviour
"""
    State for thumbnails; derivates of Images
"""

import filesystem

import PIL.Image

class Thumbnail (filesystem.File) :
    """
        A Thumbnail is a derivate of an Image, usually resized to some other size.
    """

    def __init__ (self, image, subdir, size) :
        """
            Initialize to link against the given `image`.
            
            `subdir` specifies the directory to store this thumbnail in.
            `size` determines the target resolution of the output thumbnail.
        """

        # our file path, image name inside of the given subdir
        super(Thumbnail, self).__init__(subdir, image.fsname)

        # store
        self.image = image
        self.size = size

    def stale (self) :
        """
            Tests if this thumbnail is stale
        """
        
        if self.image.older_than(self) :
            # both exist and this is newer
            return False
        
        else :
            # this thumb doesn't exist or is older
            return True

    def update (self) :
        """
            Render new output thumbnail
        """
        
        # load a copy of the PIL.Image, as .thumbnail mutates it
        thumb = self.image.pil_image.copy()

        # resample to given size
        thumb.thumbnail(self.size, resample=True)

        # and write out
        thumb.save(self.path)