[MERGE] Tools: image: reverted change introduced at rev 4440 (revid fp@openerp.com-20120916212139-2qnvuwk6sppxnp99). The purpose of image_resize is to resize an image, without cropping, without aspect ratio changes, and to apply a sharpen filter for better-looking result. Otherwise this leads to distorted images when resizing not squarred images.

bzr revid: tde@openerp.com-20121113152347-2ah6l4co3pu774jp
This commit is contained in:
Thibault Delavallée
2012-11-13 16:23:47 +01:00
+10 -14
View File
@@ -20,11 +20,10 @@
##############################################################################
import io
import sys
import StringIO
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
from random import random
# ----------------------------------------
@@ -67,18 +66,15 @@ def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', file
# check image size: do not create a thumbnail if avoiding smaller images
if avoid_if_small and image.size[0] <= size[0] and image.size[1] <= size[1]:
return base64_source
if (float(image.size[0])/image.size[1]) > (float(size[0]) / size[1]):
ibox = (size[1] * image.size[0] / image.size[1] , size[1])
deltax = max((size[1] * image.size[0] / image.size[1] - size[0]) / 2, 0)
deltay = 0
else:
ibox = (size[0],size[0] * image.size[1] / image.size[0])
deltax = 0
deltay = max((size[0] * image.size[1] / image.size[0] - size[1]) / 2, 0)
im2 = image.resize(ibox, Image.ANTIALIAS)
background = im2.crop((deltax, deltay, deltax+size[0], deltay+size[1]))
# create a thumbnail: will resize and keep ratios, then sharpen for better looking result
image.thumbnail(size, Image.ANTIALIAS)
sharpener = ImageEnhance.Sharpness(image.convert('RGB'))
image = sharpener.enhance(2.0)
# create a transparent image for background
background = Image.new('RGBA', size, (255, 255, 255, 0))
# past the resized image on the background
background.paste(image, ((size[0] - image.size[0]) / 2, (size[1] - image.size[1]) / 2))
# return an encoded image
background_stream = StringIO.StringIO()
background.save(background_stream, filetype)
return background_stream.getvalue().encode(encoding)