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.

AutorotateFilter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 34
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B apply() 0 30 6
1
"""
2
This module implement a Autorotate filter.
3
"""
4
from PIL import Image
5
from .interface import ImagineFilterInterface
6
7
8
class AutorotateFilter(ImagineFilterInterface):
9
    """
10
    Autorotate filter
11
    """
12
    def apply(self, resource):
13
        """
14
        Apply filter to resource
15
        :param resource: Image
16
        :return: Image
17
        """
18
        if not isinstance(resource, Image.Image):
19
            raise ValueError('Unknown resource format')
20
21
        if hasattr(resource, '_getexif'):
22
            exif = getattr(resource, '_getexif')()
23
            if not exif:
24
                return resource
25
26
            orientation_key = 274  # cf ExifTags
27
            if orientation_key in exif:
28
                orientation = exif[orientation_key]
29
30
                rotate_values = {
31
                    3: 180,
32
                    6: 270,
33
                    8: 90
34
                }
35
36
                if orientation in rotate_values:
37
                    resource_format = resource.format
38
                    resource = resource.rotate(rotate_values[orientation])
39
                    resource.format = resource_format
40
41
        return resource
42