Conditions | 5 |
Total Lines | 26 |
Lines | 26 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | """ |
||
24 | def apply(self, resource): |
||
25 | """ |
||
26 | Apply filter to resource. |
||
27 | :param resource: Image |
||
28 | :return: Image |
||
29 | """ |
||
30 | if not isinstance(resource, Image.Image): |
||
31 | raise ValueError('Unsupported resource format: %s' % str(type(resource))) |
||
32 | |||
33 | original_width, original_height = resource.size |
||
34 | |||
35 | if original_width > self.size[0] or original_height > self.size[1]: |
||
36 | k = original_width / original_height |
||
37 | |||
38 | if original_width >= original_height: |
||
39 | target_width = self.size[0] |
||
40 | target_height = int(target_width / k) |
||
41 | else: |
||
42 | target_height = self.size[1] |
||
43 | target_width = int(target_height * k) |
||
44 | |||
45 | resource_format = resource.format |
||
46 | resource = resource.resize((target_width, target_height), Image.ANTIALIAS) |
||
47 | resource.format = resource_format |
||
48 | |||
49 | return resource |
||
50 |