diff options
| author | Christophe Simonis <chs@odoo.com> | 2019-10-09 02:08:17 +0200 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2019-10-09 02:08:17 +0200 |
| commit | d74b4518052066446e2a1975a91ce53c745ca75a (patch) | |
| tree | c08180f88a512d15e37fbec628931f02b3825f65 /odoo/tools/image.py | |
| parent | ec162dd0cb5622ab6e56db1a63dab5f531a96f4a (diff) | |
| parent | f4105eb9c7010f8693e5d2fd20c0bd9d9cc2e483 (diff) | |
[MERGE] forward port branch 13.0 up to f4105eb9c70
Diffstat (limited to 'odoo/tools/image.py')
| -rw-r--r-- | odoo/tools/image.py | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/odoo/tools/image.py b/odoo/tools/image.py index 5819b8c7b10..f5b4b953da9 100644 --- a/odoo/tools/image.py +++ b/odoo/tools/image.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. import base64 +import binascii import io from PIL import Image @@ -9,6 +10,7 @@ from PIL import IcoImagePlugin from random import randrange +from odoo.exceptions import UserError from odoo.tools.translate import _ @@ -49,8 +51,7 @@ class ImageProcess(): :rtype: ImageProcess :raise: ValueError if `verify_resolution` is True and the image is too large - :raise: binascii.Error: if the base64 is incorrect - :raise: OSError if the image can't be identified by PIL + :raise: UserError if the base64 is incorrect or the image can't be identified by PIL """ self.base64_source = base64_source or False self.operationsCount = 0 @@ -215,8 +216,8 @@ class ImageProcess(): new_w, new_h = (new_w * h) // new_h, h # Correctly place the center of the crop. - x_offset = (w - new_w) * center_x - h_offset = (h - new_h) * center_y + x_offset = int((w - new_w) * center_x) + h_offset = int((h - new_h) * center_y) if new_w != w or new_h != h: self.image = self.image.crop((x_offset, h_offset, x_offset + new_w, h_offset + new_h)) @@ -339,10 +340,12 @@ def base64_to_image(base64_source): :return: the PIL image :rtype: PIL.Image - :raise: binascii.Error: if the base64 is incorrect - :raise: OSError if the image can't be identified by PIL + :raise: UserError if the base64 is incorrect or the image can't be identified by PIL """ - return Image.open(io.BytesIO(base64.b64decode(base64_source))) + try: + return Image.open(io.BytesIO(base64.b64decode(base64_source))) + except (OSError, binascii.Error): + raise UserError(_("This file could not be decoded as an image file. Please try with a different file.")) def image_to_base64(image, format, **params): |
