|
1 """ |
|
2 Rendering images as thumbnails/previews. |
|
3 """ |
|
4 |
|
5 import PIL.Image |
|
6 |
|
7 import os.path |
|
8 |
|
9 class RenderMachine (object) : |
|
10 """ |
|
11 RAWR! I'm the render machine! |
|
12 |
|
13 TODO: use multithreaded rendering (PIL supports it) |
|
14 """ |
|
15 |
|
16 def __init__ (self, config) : |
|
17 """ |
|
18 Use the given Configuration object's settings for rendering |
|
19 """ |
|
20 |
|
21 self.config = config |
|
22 |
|
23 def render_out (self, img, size, out_path) : |
|
24 """ |
|
25 Creates a thumbnail from the given PIL.Image of the given size, and saves it at out_path. |
|
26 """ |
|
27 |
|
28 # we need to create a copy, as .thumbnail mutates the Image |
|
29 img_out = img.copy() |
|
30 |
|
31 # then resample to given size |
|
32 img_out.thumbnail(size, resample=True) |
|
33 |
|
34 # and write out |
|
35 img_out.save(out_path) |
|
36 |
|
37 def render_img (self, img, dirname, filename) : |
|
38 """ |
|
39 Renders new thumbnails/previews from the given loaded image and filename based on the current configuration. |
|
40 |
|
41 Note: this does not check for the existance of the thumbs/previews subdirs! |
|
42 """ |
|
43 |
|
44 # once for each size |
|
45 for subdir, size in ( |
|
46 (self.config.thumb_dir, self.config.thumb_size), |
|
47 (self.config.preview_dir, self.config.preview_size) |
|
48 ) : |
|
49 # resize and write out |
|
50 self.render_out(img, size, os.path.join(dirname, subdir, filename) |
|
51 |