Skip to content
Snippets Groups Projects
Commit 3d5774f6 authored by jard@odoo.com's avatar jard@odoo.com
Browse files

[FIX] tools.image: Allow conversion of P to JPEG


While upgrade of a customer database, A P-type image
was being converted to JPEG image. It didn't allow to save
as direct conversion from P-type(palette) to JPEG. So, fix
covers conversion of such corner cases to first convert it
to RGB and then RGB can convert it to desired output format
opw-3043418

closes odoo/odoo#106859

X-original-commit: a44a0359
Signed-off-by: default avatarSébastien Theys (seb) <seb@odoo.com>
parent 71229be0
No related branches found
No related tags found
No related merge requests found
...@@ -314,3 +314,9 @@ class TestImage(TransactionCase): ...@@ -314,3 +314,9 @@ class TestImage(TransactionCase):
self._assertAlmostEqualSequence(fixed_image.getpixel((size - 1, 0)), expected[1]) # top/right self._assertAlmostEqualSequence(fixed_image.getpixel((size - 1, 0)), expected[1]) # top/right
self._assertAlmostEqualSequence(fixed_image.getpixel((0, size - 1)), expected[2]) # bottom/left self._assertAlmostEqualSequence(fixed_image.getpixel((0, size - 1)), expected[2]) # bottom/left
self._assertAlmostEqualSequence(fixed_image.getpixel((size - 1, size - 1)), expected[3]) # bottom/right self._assertAlmostEqualSequence(fixed_image.getpixel((size - 1, size - 1)), expected[3]) # bottom/right
def test_ptype_image_to_jpeg(self):
"""converts to RGB when saving as JPEG"""
image1 = Image.new('P', (1, 1), color='red')
image2 = Image.new('RGB', (1, 1), color='red')
self.assertEqual(tools.image.image_apply_opt(image1, 'JPEG'), tools.image.image_apply_opt(image2, 'JPEG'))
...@@ -460,6 +460,8 @@ def image_apply_opt(image, format, **params): ...@@ -460,6 +460,8 @@ def image_apply_opt(image, format, **params):
:return: the image formatted :return: the image formatted
:rtype: bytes :rtype: bytes
""" """
if format == 'JPEG' and image.mode not in ['1', 'L', 'RGB']:
image = image.convert("RGB")
stream = io.BytesIO() stream = io.BytesIO()
image.save(stream, format=format, **params) image.save(stream, format=format, **params)
return stream.getvalue() return stream.getvalue()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment