degal/render.py
changeset 51 0f39cb5e4b11
child 57 8d06e0283b88
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/render.py	Thu Jun 04 11:22:52 2009 +0300
@@ -0,0 +1,51 @@
+"""
+    Rendering images as thumbnails/previews.
+"""
+
+import PIL.Image
+
+import os.path
+
+class RenderMachine (object) :
+    """
+        RAWR! I'm the render machine!
+
+        TODO: use multithreaded rendering (PIL supports it)
+    """
+
+    def __init__ (self, config) :
+        """
+            Use the given Configuration object's settings for rendering
+        """
+
+        self.config = config
+
+    def render_out (self, img, size, out_path) :
+        """
+            Creates a thumbnail from the given PIL.Image of the given size, and saves it at out_path.
+        """
+
+        # we need to create a copy, as .thumbnail mutates the Image
+        img_out = img.copy()
+
+        # then resample to given size
+        img_out.thumbnail(size, resample=True)
+
+        # and write out
+        img_out.save(out_path)
+
+    def render_img (self, img, dirname, filename) :
+        """
+            Renders new thumbnails/previews from the given loaded image and filename based on the current configuration.
+            
+            Note: this does not check for the existance of the thumbs/previews subdirs!
+        """
+
+        # once for each size
+        for subdir, size in (
+            (self.config.thumb_dir, self.config.thumb_size),
+            (self.config.preview_dir, self.config.preview_size)
+        ) :
+            # resize and write out
+            self.render_out(img, size, os.path.join(dirname, subdir, filename)
+