terom@85: """ terom@85: State for thumbnails; derivates of Images terom@85: """ terom@85: terom@85: import filesystem terom@85: terom@85: import PIL.Image terom@85: terom@85: class Thumbnail (filesystem.File) : terom@85: """ terom@85: A Thumbnail is a derivate of an Image, usually resized to some other size. terom@85: """ terom@85: terom@85: def __init__ (self, image, subdir, size) : terom@85: """ terom@85: Initialize to link against the given `image`. terom@85: terom@85: `subdir` specifies the directory to store this thumbnail in. terom@85: `size` determines the target resolution of the output thumbnail. terom@85: """ terom@85: terom@85: # our file path terom@85: # XXX: this should be the binary fsname, not name terom@85: super(Thumbnail, self).__init__(subdir.subnode(image.name)) terom@85: terom@85: # store terom@85: self.image = image terom@85: self.size = size terom@85: terom@85: def stale (self) : terom@85: """ terom@85: Tests if this thumbnail is stale terom@85: """ terom@85: terom@85: return self.older_than(self.image) terom@85: terom@85: def update (self) : terom@85: """ terom@85: Render new output thumbnail terom@85: """ terom@85: terom@85: # load a copy of the PIL.Image, as .thumbnail mutates it terom@85: thumb = self.image.pil_image.copy() terom@85: terom@85: # resample to given size terom@85: thumb.thumbnail(self.size, resample=True) terom@85: terom@85: # and write out terom@85: thumb.save(self.path) terom@85: