|
45 | 45 | from pilkit.processors import Transpose |
46 | 46 | from pilkit.utils import save_image |
47 | 47 |
|
| 48 | +try: |
| 49 | + # Pillow 6.0 added a class allowing to write Exif data |
| 50 | + from PIL.Image import Exif |
| 51 | +except ImportError: |
| 52 | + Exif = None |
| 53 | + |
48 | 54 | from . import signals, utils |
49 | 55 | from .settings import Status, get_thumb |
50 | 56 |
|
@@ -97,25 +103,36 @@ def generate_image(source, outname, settings, options=None): |
97 | 103 | img = _read_image(source) |
98 | 104 | original_format = img.format |
99 | 105 |
|
100 | | - if settings['copy_exif_data'] and settings['autorotate_images']: |
101 | | - logger.warning("The 'autorotate_images' and 'copy_exif_data' settings " |
102 | | - "are not compatible because Sigal can't save the " |
103 | | - "modified Orientation tag.") |
104 | | - |
105 | 106 | # Preserve EXIF data |
106 | | - if settings['copy_exif_data'] and _has_exif_tags(img): |
| 107 | + copy_exif = settings['copy_exif_data'] and _has_exif_tags(img) |
| 108 | + if copy_exif: |
107 | 109 | if options is not None: |
108 | 110 | options = deepcopy(options) |
109 | 111 | else: |
110 | 112 | options = {} |
111 | | - options['exif'] = img.info['exif'] |
| 113 | + |
| 114 | + if Exif is not None: |
| 115 | + options['exif'] = img.getexif() |
| 116 | + else: |
| 117 | + options['exif'] = img.info['exif'] |
112 | 118 |
|
113 | 119 | # Rotate the img, and catch IOError when PIL fails to read EXIF |
114 | 120 | if settings['autorotate_images']: |
115 | 121 | try: |
116 | 122 | img = Transpose().process(img) |
117 | 123 | except (OSError, IndexError): |
118 | 124 | pass |
| 125 | + else: |
| 126 | + if copy_exif and 'exif' in options: |
| 127 | + if Exif is not None: |
| 128 | + # reset the orientation flag |
| 129 | + options['exif'][0x0112] = 1 |
| 130 | + else: |
| 131 | + logger.warning( |
| 132 | + "'autorotate_images' and 'copy_exif_data' settings " |
| 133 | + "are not compatible when using Pillow < 6.0, because " |
| 134 | + "Sigal can't save the modified Orientation tag." |
| 135 | + ) |
119 | 136 |
|
120 | 137 | # Resize the image |
121 | 138 | if settings['img_processor']: |
|
0 commit comments