| Total Complexity | 6 |
| Total Lines | 34 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | """ |
||
| 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 |