Skip to content

Commit 8f5c633

Browse files
committed
wip - update exif orientation when using pillow 6.0+
1 parent 2d823c6 commit 8f5c633

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

sigal/image.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
from pilkit.processors import Transpose
4646
from pilkit.utils import save_image
4747

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+
4854
from . import signals, utils
4955
from .settings import Status, get_thumb
5056

@@ -97,25 +103,36 @@ def generate_image(source, outname, settings, options=None):
97103
img = _read_image(source)
98104
original_format = img.format
99105

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-
105106
# 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:
107109
if options is not None:
108110
options = deepcopy(options)
109111
else:
110112
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']
112118

113119
# Rotate the img, and catch IOError when PIL fails to read EXIF
114120
if settings['autorotate_images']:
115121
try:
116122
img = Transpose().process(img)
117123
except (OSError, IndexError):
118124
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+
)
119136

120137
# Resize the image
121138
if settings['img_processor']:

0 commit comments

Comments
 (0)