equal
deleted
inserted
replaced
1 """ |
|
2 Rendering images as thumbnails/previews. |
|
3 """ |
|
4 |
|
5 import PIL.Image |
|
6 |
|
7 class RenderMachine (object) : |
|
8 """ |
|
9 RAWR! I'm the render machine! |
|
10 |
|
11 TODO: use multithreaded rendering (PIL supports it) |
|
12 """ |
|
13 |
|
14 def __init__ (self, config) : |
|
15 """ |
|
16 Use the given Configuration object's settings for rendering |
|
17 """ |
|
18 |
|
19 self.config = config |
|
20 |
|
21 def render_out (self, image, size, file) : |
|
22 """ |
|
23 Creates a thumbnail from the given Image of the given `size`, and saves it at `out`. |
|
24 |
|
25 Returns the file for convenience |
|
26 """ |
|
27 |
|
28 # load the PIL.Image |
|
29 img = image.image |
|
30 |
|
31 # we need to create a copy, as .thumbnail mutates the Image |
|
32 img_out = img.copy() |
|
33 |
|
34 # then resample to given size |
|
35 img_out.thumbnail(size, resample=True) |
|
36 |
|
37 # and write out |
|
38 img_out.save(file.path) |
|
39 |
|
40 return file |
|
41 |
|
42 def render_lazy (self, image, size, out) : |
|
43 """ |
|
44 Renders the given image with render_out if `out` does not yet exist or is older than image. |
|
45 """ |
|
46 |
|
47 # stat the output file |
|
48 out_stat = out.stat(soft=True) |
|
49 |
|
50 # compare |
|
51 if not out_stat or out_stat.st_mtime < image.stat.st_mtime : |
|
52 # render anew |
|
53 return self.render_out(image, size, out) |
|
54 |
|
55 else : |
|
56 # already rendered |
|
57 return out |
|