[REV] tools: image: reverted changes introduced at rev 4440. The purpose of image_resize is to resize an image, without cropping, without aspect ratio changes. If another image management function is needed, please implement a new one with matching comments and behavior.

bzr revid: tde@openerp.com-20121113141752-1rbsais66ooi89jp
This commit is contained in:
Thibault Delavallée 2012-11-13 15:17:52 +01:00
parent 4d51fd1c02
commit d36f972f96
1 changed files with 9 additions and 13 deletions

View File

@ -20,7 +20,6 @@
##############################################################################
import io
import sys
import StringIO
from PIL import Image
@ -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)
image.convert('RGB')
image = image.filter(ImageFilter.SHARPEN)
# 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)