From 2688ac29a0ac6479f317fea4c4ba1f397ebfb0f3 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 17 Mar 2014 09:41:23 +0100 Subject: [PATCH] [FIX] keep image type intact during resize Unless specficially requested, the output image should have the same file type as the input (JPEG -> JPEG, PNG -> PNG), everything should not be converted to PNG by default. bzr revid: xmo@openerp.com-20140317084123-wvn8r4ot79610k1r --- openerp/tools/image.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/openerp/tools/image.py b/openerp/tools/image.py index 196ace2da60..c5e5b396922 100644 --- a/openerp/tools/image.py +++ b/openerp/tools/image.py @@ -32,7 +32,7 @@ from random import randint # Image resizing # ---------------------------------------- -def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', filetype='PNG', avoid_if_small=False): +def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', filetype=None, avoid_if_small=False): """ Function to resize an image. The image will be resized to the given size, while keeping the aspect ratios, and holes in the image will be filled with transparent background. The image will not be stretched if @@ -58,7 +58,8 @@ def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', file height mean an automatically computed value based respectivelly on height or width of the source image. :param encoding: the output encoding - :param filetype: the output filetype + :param filetype: the output filetype, by default the source image's + :type filetype: str, any PIL image format (supported for creation) :param avoid_if_small: do not resize if image height and width are smaller than the expected size. """ @@ -68,6 +69,8 @@ def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', file return base64_source image_stream = StringIO.StringIO(base64_source.decode(encoding)) image = Image.open(image_stream) + # store filetype here, as Image.new below will lose image.format + filetype = filetype or image.format asked_width, asked_height = size if asked_width is None: