degal/thumbnail.py
changeset 85 7da934333469
child 95 3b00bd676fc9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/thumbnail.py	Thu Jun 11 00:36:19 2009 +0300
@@ -0,0 +1,50 @@
+"""
+    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
+        # XXX: this should be the binary fsname, not name
+        super(Thumbnail, self).__init__(subdir.subnode(image.name))
+
+        # store
+        self.image = image
+        self.size = size
+
+    def stale (self) :
+        """
+            Tests if this thumbnail is stale
+        """
+        
+        return self.older_than(self.image)
+
+    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)
+