Skip to content

Commit

Permalink
[ADD] tools.image_save_for_web() helper for image compression
Browse files Browse the repository at this point in the history
  • Loading branch information
amigrave committed Sep 16, 2014
1 parent 8ccd4c6 commit 89362bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
11 changes: 2 additions & 9 deletions addons/website/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import openerp
from openerp.osv import orm, osv, fields
from openerp.tools import html_escape as escape, ustr, image_resize_and_sharpen
from openerp.tools import html_escape as escape, ustr, image_resize_and_sharpen, image_save_for_web
from openerp.tools.safe_eval import safe_eval
from openerp.addons.web.http import request

Expand Down Expand Up @@ -588,16 +588,9 @@ def _image(self, cr, uid, model, id, field, response, max_width=maxint, max_heig
if w < max_w and h < max_h:
response.data = data
else:
image_format = image.format
size = (max_w, max_h)
image = image_resize_and_sharpen(image, size)

if image_format == 'PNG':
image.convert('P').save(response.stream, optimize=True, format='PNG')
elif image.format == 'JPEG':
image.save(response.stream, quality=80, optimize=True, format='JPG')
else:
image.save(response.stream, format=image_format)
image_save_for_web(image, response.stream)
# invalidate content-length computed by make_conditional as
# writing to response.stream does not do it (as of werkzeug 0.9.3)
del response.headers['Content-Length']
Expand Down
22 changes: 22 additions & 0 deletions openerp/tools/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ def image_resize_and_sharpen(image, size, factor=2.0):
image.paste(resized_image, ((size[0] - resized_image.size[0]) / 2, (size[1] - resized_image.size[1]) / 2))
return image

def image_save_for_web(image, fp=None):
"""
Save image optimized for web usage.
:param image: PIL.Image.Image()
:param fp: File name or file object. If not specified, a bytestring is returned.
"""
opt = dict(format=image.format)
if image.format == 'PNG':
opt.update(optimize=True)
if image.mode != 'P':
# Floyd Steinberg dithering by default
image = image.convert('RGBA').convert('P', palette=Image.WEB, colors=256)
elif image.format == 'JPEG':
opt.update(optimize=True, quality=80)
if fp:
image.save(fp, **opt)
else:
img = StringIO.StringIO()
image.save(img, **opt)
return img.getvalue()

def image_resize_image_big(base64_source, size=(1204, 1024), encoding='base64', filetype=None, avoid_if_small=True):
""" Wrapper on image_resize_image, to resize images larger than the standard
'big' image size: 1024x1024px.
Expand Down

0 comments on commit 89362bd

Please sign in to comment.