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@95: # our file path, image name inside of the given subdir terom@95: super(Thumbnail, self).__init__(subdir, image.fsname) 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@95: if self.image.older_than(self) : terom@95: # both exist and this is newer terom@95: return False terom@95: terom@95: else : terom@95: # this thumb doesn't exist or is older terom@95: return True 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@95: