| @@ 9-49 (lines=41) @@ | ||
| 6 | from PIL import Image |
|
| 7 | ||
| 8 | ||
| 9 | class DownscaleFilter(ImagineFilterInterface): |
|
| 10 | """ |
|
| 11 | Downscale filter |
|
| 12 | """ |
|
| 13 | size = None |
|
| 14 | ||
| 15 | def __init__(self, **kwargs): |
|
| 16 | """ |
|
| 17 | :param kwargs: dict |
|
| 18 | """ |
|
| 19 | if 'max' in kwargs and isinstance(kwargs['max'], list) and len(kwargs['max']) == 2: |
|
| 20 | self.size = kwargs.get('max') |
|
| 21 | else: |
|
| 22 | raise ValueError('Unsupported configuration') |
|
| 23 | ||
| 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 | ||
| @@ 8-48 (lines=41) @@ | ||
| 5 | from PIL import Image |
|
| 6 | ||
| 7 | ||
| 8 | class UpscaleFilter(ImagineFilterInterface): |
|
| 9 | """ |
|
| 10 | Upscale filter |
|
| 11 | """ |
|
| 12 | size = None |
|
| 13 | ||
| 14 | def __init__(self, **kwargs): |
|
| 15 | """ |
|
| 16 | :param kwargs: dict |
|
| 17 | """ |
|
| 18 | if 'min' in kwargs and isinstance(kwargs['min'], list) and len(kwargs['min']) == 2: |
|
| 19 | self.size = kwargs.get('min') |
|
| 20 | else: |
|
| 21 | raise ValueError('Unsupported configuration') |
|
| 22 | ||
| 23 | def apply(self, resource): |
|
| 24 | """ |
|
| 25 | Apply filter to resource. |
|
| 26 | :param resource: Image |
|
| 27 | :return: Image |
|
| 28 | """ |
|
| 29 | if not isinstance(resource, Image.Image): |
|
| 30 | raise ValueError('Unsupported resource format: %s' % str(type(resource))) |
|
| 31 | ||
| 32 | original_width, original_height = resource.size |
|
| 33 | ||
| 34 | if original_width < self.size[0] and original_height < self.size[1]: |
|
| 35 | k = original_width / float(original_height) |
|
| 36 | ||
| 37 | if original_width >= original_height: |
|
| 38 | target_width = self.size[0] |
|
| 39 | target_height = int(target_width / k) |
|
| 40 | else: |
|
| 41 | target_height = self.size[1] |
|
| 42 | target_width = int(target_height * k) |
|
| 43 | ||
| 44 | resource_format = resource.format |
|
| 45 | resource = resource.resize((target_width, target_height), Image.ANTIALIAS) |
|
| 46 | resource.format = resource_format |
|
| 47 | ||
| 48 | return resource |
|
| 49 | ||