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.
Completed
Push — master ( 9f3e8d...05150b )
by Dmitry
01:01
created

ThumbnailFilter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 130
Duplicated Lines 0 %
Metric Value
wmc 26
dl 0
loc 130
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __init__() 0 14 6
B crop_sizes() 0 24 3
F inset_sizes() 0 33 10
B outbound_sizes() 0 24 4
A apply() 0 22 3
1
"""
2
This module implement a Thumbnail filter.
3
"""
4
from PIL import Image
5
from .interface import ImagineFilterInterface
6
7
8
class ThumbnailFilter(ImagineFilterInterface):
9
    """
10
    Thumbnail filter
11
    """
12
    modes = ['inset', 'outbound']
13
    mode = 'inset'
14
    width = 0
15
    height = 0
16
17
    def __init__(self, **kwargs):
18
        """
19
        Filter initialization
20
        :param kwargs: parameters
21
        """
22
        if 'mode' in kwargs and kwargs['mode'] in self.modes:
23
            self.mode = kwargs.pop('mode')
24
25
        if 'size' in kwargs and isinstance(kwargs['size'], list) and len(kwargs['size']) == 2:
26
            size = kwargs.pop('size', [0, 0])
27
            self.width = size[0]
28
            self.height = size[1]
29
        else:
30
            raise ValueError('Thumbnail size is not set.')
31
32
    def apply(self, resource):
33
        """
34
        Apply filter to resource
35
        :param resource: Image
36
        :return: Image
37
        """
38
        if not isinstance(resource, Image.Image):
39
            raise ValueError('Unknown resource format')
40
41
        original_width, original_height = resource.size
42
43
        if self.mode == 'outbound':
44
            target_width, target_height = self.outbound_sizes(original_width, original_height, self.width, self.height)
45
            resource = resource.resize((target_width, target_height), Image.ANTIALIAS)
46
47
            crop_sizes = self.crop_sizes(target_width, target_height, self.width, self.height)
48
            resource = resource.crop(crop_sizes)
49
        else:
50
            target_width, target_height = self.inset_sizes(original_width, original_height, self.width, self.height)
51
            resource = resource.resize((target_width, target_height), Image.ANTIALIAS)
52
53
        return resource
54
55
    @classmethod
56
    def inset_sizes(cls, original_width, original_height, target_width, target_height):
57
        """
58
        Calculate new image sizes for inset mode
59
        :param original_width: int
60
        :param original_height: int
61
        :param target_width: int
62
        :param target_height: int
63
        :return: tuple(int, int)
64
        """
65
        if target_width >= original_width and target_height >= original_height:
66
            target_width = float(original_width)
67
            target_height = original_height
68
69
        elif target_width <= original_width and target_height >= original_height:
70
            k = original_width / float(target_width)
71
            target_height = int(original_height / k)
72
73
        elif target_width >= original_width and target_height <= original_height:
74
            k = original_height / float(target_height)
75
            target_width = int(original_width / k)
76
77
        elif target_width < original_width and target_height < original_height:
78
            k = original_width / float(original_height)
79
            k_w = original_width / float(target_width)
80
            k_h = original_height / float(target_height)
81
82
            if k_w >= k_h:
83
                target_height = int(target_width / k)
84
            else:
85
                target_width = int(target_height * k)
86
87
        return target_width, target_height
88
89
    @classmethod
90
    def outbound_sizes(cls, original_width, original_height, target_width, target_height):
91
        """
92
        Calculate new image sizes for outbound mode
93
        :param original_width: int
94
        :param original_height: int
95
        :param target_width: int
96
        :param target_height: int
97
        :return: tuple(int, int)
98
        """
99
        if target_width <= original_width and target_height <= original_height:
100
            k = original_width / float(original_height)
101
            k_w = original_width / float(target_width)
102
            k_h = original_height / float(target_height)
103
104
            if k_w > k_h:
105
                target_width = int(target_height * k)
106
            else:
107
                target_height = int(target_width / k)
108
        else:
109
            target_width = original_width
110
            target_height = original_height
111
112
        return target_width, target_height
113
114
    @classmethod
115
    def crop_sizes(cls, original_width, original_height, target_width, target_height):
116
        """
117
        Calculate crop parameters for outbound mode
118
        :param original_width: int
119
        :param original_height: int
120
        :param target_width: int
121
        :param target_height: int
122
        :return: tuple(int, int, int, int)
123
        """
124
        if target_width < original_width:
125
            left = abs(original_width - target_width) / 2
126
            right = left + target_width
127
        else:
128
            left = 0
129
            right = original_width
130
        if target_height < original_height:
131
            upper = abs(original_height - target_height) / 2
132
            lower = upper + target_height
133
        else:
134
            upper = 0
135
            lower = original_height
136
137
        return left, upper, right, lower
138