[FIX] Regression in image resize helper (Fixes #2529)
authorFabien Meghazi <fme@openerp.com>
Mon, 27 Oct 2014 16:55:08 +0000 (17:55 +0100)
committerFabien Meghazi <fme@openerp.com>
Mon, 27 Oct 2014 16:55:08 +0000 (17:55 +0100)
Commit 57ad514b makes the function preserve the aspect ration of the
original picture. Error of mine because the expected behavior was to
lose it for kanban view purpose.

For backward compatibility sake, this commit will keep the old behavior
by default.

addons/website/models/website.py
openerp/tools/image.py

index 541a75a..25dc303 100644 (file)
@@ -591,7 +591,7 @@ class website(osv.osv):
             response.data = data
         else:
             size = (max_w, max_h)
-            img = image_resize_and_sharpen(image, size)
+            img = image_resize_and_sharpen(image, size, preserve_aspect_ratio=True)
             image_save_for_web(img, response.stream, format=image.format)
             # invalidate content-length computed by make_conditional as
             # writing to response.stream does not do it (as of werkzeug 0.9.3)
index aa110d7..0b02d27 100644 (file)
@@ -96,19 +96,21 @@ def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', file
     image.save(background_stream, filetype)
     return background_stream.getvalue().encode(encoding)
 
-def image_resize_and_sharpen(image, size, factor=2.0):
+def image_resize_and_sharpen(image, size, preserve_aspect_ratio=False, factor=2.0):
     """
         Create a thumbnail by resizing while keeping ratio.
         A sharpen filter is applied for a better looking result.
 
         :param image: PIL.Image.Image()
         :param size: 2-tuple(width, height)
+        :param preserve_aspect_ratio: boolean (default: False)
         :param factor: Sharpen factor (default: 2.0)
     """
     if image.mode != 'RGBA':
         image = image.convert('RGBA')
     image.thumbnail(size, Image.ANTIALIAS)
-    size = image.size
+    if preserve_aspect_ratio:
+        size = image.size
     sharpener = ImageEnhance.Sharpness(image)
     resized_image = sharpener.enhance(factor)
     # create a transparent image for background and paste the image on it