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.

CropFilter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B apply() 0 33 6
B __init__() 0 14 5
1
"""
2
This module implement a Crop filter.
3
"""
4
from .interface import ImagineFilterInterface
5
from PIL import Image
6
7
8
class CropFilter(ImagineFilterInterface):
9
    """
10
    Crop filter
11
    """
12
    start = None
13
    size = None
14
15
    def __init__(self, start, size):
16
        """
17
        :param start: list
18
        :param size: list
19
        """
20
        if isinstance(start, list) and len(start) == 2:
21
            self.start = start
22
        else:
23
            raise ValueError('Unknown start position.')
24
25
        if isinstance(size, list) and len(size) == 2:
26
            self.size = size
27
        else:
28
            raise ValueError('Unknown image size.')
29
30
    def apply(self, resource):
31
        """
32
        Apply filter to resource
33
        :param resource: Image
34
        :return: Image
35
        """
36
        if not isinstance(resource, Image.Image):
37
            raise ValueError('Unknown resource format')
38
39
        original_width, original_height = resource.size
40
41
        if self.start[0] < original_width or self.start[1] < original_height:
42
            left = self.start[0]
43
            upper = self.start[1]
44
        else:
45
            left = 0
46
            upper = 0
47
48
        right = self.start[0] + self.size[0]
49
        lower = self.start[1] + self.size[1]
50
51
        resource_format = resource.format
52
        resource = resource.crop(
53
            (
54
                left,
55
                upper,
56
                right if right < original_width else original_width,
57
                lower if lower < original_height else original_height
58
            )
59
        )
60
        resource.format = resource_format
61
62
        return resource
63