summaryrefslogtreecommitdiff
path: root/odoo/tools/image.py
diff options
context:
space:
mode:
Diffstat (limited to 'odoo/tools/image.py')
-rw-r--r--odoo/tools/image.py17
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):