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.

ThumbnailFilter   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 136
rs 10
wmc 24

5 Methods

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