equal
deleted
inserted
replaced
9 class Thumbnail (filesystem.File) : |
9 class Thumbnail (filesystem.File) : |
10 """ |
10 """ |
11 A Thumbnail is a derivate of an Image, usually resized to some other size. |
11 A Thumbnail is a derivate of an Image, usually resized to some other size. |
12 """ |
12 """ |
13 |
13 |
14 def __init__ (self, image, subdir, size) : |
14 def __init__ (self, image, subdir, target_size) : |
15 """ |
15 """ |
16 Initialize to link against the given `image`. |
16 Initialize to link against the given `image`. |
17 |
17 |
18 `subdir` specifies the directory to store this thumbnail in. |
18 `subdir` specifies the directory to store this thumbnail in. |
19 `size` determines the target resolution of the output thumbnail. |
19 `target_size` determines the target resolution of the output thumbnail. |
20 """ |
20 """ |
21 |
21 |
22 # our file path, image name inside of the given subdir |
22 # our file path, image name inside of the given subdir |
23 super(Thumbnail, self).__init__(subdir, image.fsname) |
23 super(Thumbnail, self).__init__(subdir, image.fsname) |
24 |
24 |
25 # store |
25 # store |
26 self.image = image |
26 self.image = image |
27 self.size = size |
27 self.target_size = target_size |
28 |
28 |
29 def stale (self) : |
29 def stale (self) : |
30 """ |
30 """ |
31 Tests if this thumbnail is stale |
31 Tests if this thumbnail is stale |
32 """ |
32 """ |
36 return False |
36 return False |
37 |
37 |
38 else : |
38 else : |
39 # this thumb doesn't exist or is older |
39 # this thumb doesn't exist or is older |
40 return True |
40 return True |
|
41 |
|
42 @property |
|
43 def size (self) : |
|
44 """ |
|
45 Compute the *real* size of this thumbnail, from the image's actual size and our target size. |
|
46 |
|
47 Preserves the aspect ratio etc. |
|
48 """ |
|
49 |
|
50 # real image size |
|
51 img_width, img_height = self.image.img_size |
|
52 |
|
53 # target size |
|
54 thumb_width, thumb_height = self.target_size |
|
55 |
|
56 # calc new size, preserving aspect ratio |
|
57 if img_width > thumb_width : |
|
58 height = max(img_height * thumb_width / img_width, 1) |
|
59 width = thumb_width |
|
60 |
|
61 if img_height > thumb_height : |
|
62 width = max(img_width * thumb_height / img_height, 1) |
|
63 height = thumb_height |
|
64 |
|
65 return width, height |
41 |
66 |
42 def update (self) : |
67 def update (self) : |
43 """ |
68 """ |
44 Render new output thumbnail |
69 Render new output thumbnail. |
45 """ |
70 """ |
46 |
|
47 # load a copy of the PIL.Image, as .thumbnail mutates it |
|
48 thumb = self.image.pil_image.copy() |
|
49 |
71 |
50 # resample to given size |
72 # create resized copy of main image, using our size |
51 thumb.thumbnail(self.size, resample=True) |
73 thumb = self.image.img.resize(self.size, resample=PIL.Image.ANTIALIAS) |
52 |
74 |
53 # and write out |
75 # write it out |
54 thumb.save(self.path) |
76 thumb.save(self.path) |
55 |
77 |