GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DownscaleFilter.apply()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 26

Duplication

Lines 26
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 26
loc 26
rs 8.0894
1
"""
2
This module implement a downscale filter.
3
"""
4
from __future__ import division
5
from .interface import ImagineFilterInterface
6
from PIL import Image
7
8
9 View Code Duplication
class DownscaleFilter(ImagineFilterInterface):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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