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