"""
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)