degal/thumbnail.py
changeset 85 7da934333469
child 95 3b00bd676fc9
equal deleted inserted replaced
84:891545a38a2b 85:7da934333469
       
     1 """
       
     2     State for thumbnails; derivates of Images
       
     3 """
       
     4 
       
     5 import filesystem
       
     6 
       
     7 import PIL.Image
       
     8 
       
     9 class Thumbnail (filesystem.File) :
       
    10     """
       
    11         A Thumbnail is a derivate of an Image, usually resized to some other size.
       
    12     """
       
    13 
       
    14     def __init__ (self, image, subdir, size) :
       
    15         """
       
    16             Initialize to link against the given `image`.
       
    17             
       
    18             `subdir` specifies the directory to store this thumbnail in.
       
    19             `size` determines the target resolution of the output thumbnail.
       
    20         """
       
    21 
       
    22         # our file path
       
    23         # XXX: this should be the binary fsname, not name
       
    24         super(Thumbnail, self).__init__(subdir.subnode(image.name))
       
    25 
       
    26         # store
       
    27         self.image = image
       
    28         self.size = size
       
    29 
       
    30     def stale (self) :
       
    31         """
       
    32             Tests if this thumbnail is stale
       
    33         """
       
    34         
       
    35         return self.older_than(self.image)
       
    36 
       
    37     def update (self) :
       
    38         """
       
    39             Render new output thumbnail
       
    40         """
       
    41         
       
    42         # load a copy of the PIL.Image, as .thumbnail mutates it
       
    43         thumb = self.image.pil_image.copy()
       
    44 
       
    45         # resample to given size
       
    46         thumb.thumbnail(self.size, resample=True)
       
    47 
       
    48         # and write out
       
    49         thumb.save(self.path)
       
    50