"""
Rendering images as thumbnails/previews.
"""
import PIL.Image
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, image, size, file) :
"""
Creates a thumbnail from the given Image of the given `size`, and saves it at `out`.
Returns the file for convenience
"""
# load the PIL.Image
img = image.image
# 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(file.path)
return file
def render_lazy (self, image, size, out) :
"""
Renders the given image with render_out if `out` does not yet exist or is older than image.
"""
# stat the output file
out_stat = out.stat(soft=True)
# compare
if not out_stat or out_stat.st_mtime < image.stat.st_mtime :
# render anew
return self.render_out(image, size, out)
else :
# already rendered
return out