[FIX] tools: prevent crashes with Pillow >= 4.2.0
From Pillow 4.2, it is forbidden to save RGBA images as JPEG
( e4d6223c94
)
A crash was occurring when loading demo JPGs as
image_resize_and_sharpen() was silently changing image mode to RGBA.
Now we ensure that we return the original image mode.
We also avoid crashes when converting from PNG to JPG
This commit is contained in:
parent
a1f1f85be2
commit
63ad8b9597
|
@ -93,7 +93,7 @@ def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', file
|
|||
|
||||
if image.size != size:
|
||||
image = image_resize_and_sharpen(image, size)
|
||||
if image.mode not in ["1", "L", "P", "RGB", "RGBA"]:
|
||||
if image.mode not in ["1", "L", "P", "RGB", "RGBA"] or (filetype == 'JPEG' and image.mode == 'RGBA'):
|
||||
image = image.convert("RGB")
|
||||
|
||||
background_stream = StringIO.StringIO()
|
||||
|
@ -110,6 +110,7 @@ def image_resize_and_sharpen(image, size, preserve_aspect_ratio=False, factor=2.
|
|||
:param preserve_aspect_ratio: boolean (default: False)
|
||||
:param factor: Sharpen factor (default: 2.0)
|
||||
"""
|
||||
origin_mode = image.mode
|
||||
if image.mode != 'RGBA':
|
||||
image = image.convert('RGBA')
|
||||
image.thumbnail(size, Image.ANTIALIAS)
|
||||
|
@ -120,6 +121,8 @@ def image_resize_and_sharpen(image, size, preserve_aspect_ratio=False, factor=2.
|
|||
# create a transparent image for background and paste the image on it
|
||||
image = Image.new('RGBA', size, (255, 255, 255, 0))
|
||||
image.paste(resized_image, ((size[0] - resized_image.size[0]) / 2, (size[1] - resized_image.size[1]) / 2))
|
||||
if image.mode != origin_mode:
|
||||
image = image.convert(origin_mode)
|
||||
return image
|
||||
|
||||
def image_save_for_web(image, fp=None, format=None):
|
||||
|
|
Loading…
Reference in New Issue