Allow change quality when RESIZE

This commit is contained in:
Cássio Botaro committed 2015-08-13 02:12:49 -03:00
1 parent a378ab3e51
commit 8f7acd8154
1 file changed
+12 -7
+12 -7
View File
@@ -6,15 +6,17 @@
# #
# Given the model # Given the model
# #
# db.define_table("table_name", Field("picture", "upload"), Field("thumbnail", "upload")) # db.define_table("table_name", Field("picture", "upload"),
# Field("thumbnail", "upload"))
# #
# # to resize the picture on upload # to resize the picture on upload
# #
# from images import RESIZE # from images import RESIZE
# #
# db.table_name.picture.requires = RESIZE(200, 200) # db.table_name.picture.requires = RESIZE(200, 200)
# #
# # to store original image in picture and create a thumbnail in 'thumbnail' field # to store original image in picture and create a thumbnail
# in 'thumbnail' field
# #
# from images import THUMB # from images import THUMB
# db.table_name.thumbnail.compute = lambda row: THUMB(row.picture, 200, 200) # db.table_name.thumbnail.compute = lambda row: THUMB(row.picture, 200, 200)
@@ -24,8 +26,11 @@ from gluon import current
class RESIZE(object): class RESIZE(object):
def __init__(self, nx=160, ny=80, error_message=' image resize'):
(self.nx, self.ny, self.error_message) = (nx, ny, error_message) def __init__(self, nx=160, ny=80, quality=100,
error_message=' image resize'):
(self.nx, self.ny, self.quality, self.error_message) = (
nx, ny, quality, error_message)
def __call__(self, value): def __call__(self, value):
if isinstance(value, str) and len(value) == 0: if isinstance(value, str) and len(value) == 0:
@@ -36,7 +41,7 @@ class RESIZE(object):
img = Image.open(value.file) img = Image.open(value.file)
img.thumbnail((self.nx, self.ny), Image.ANTIALIAS) img.thumbnail((self.nx, self.ny), Image.ANTIALIAS)
s = cStringIO.StringIO() s = cStringIO.StringIO()
img.save(s, 'JPEG', quality=100) img.save(s, 'JPEG', quality=self.quality)
s.seek(0) s.seek(0)
value.file = s value.file = s
except: except:
@@ -51,7 +56,7 @@ def THUMB(image, nx=120, ny=120, gae=False, name='thumb'):
request = current.request request = current.request
from PIL import Image from PIL import Image
import os import os
img = Image.open(os.path.join(request.folder,'uploads',image)) img = Image.open(os.path.join(request.folder, 'uploads', image))
img.thumbnail((nx, ny), Image.ANTIALIAS) img.thumbnail((nx, ny), Image.ANTIALIAS)
root, ext = os.path.splitext(image) root, ext = os.path.splitext(image)
thumb = '%s_%s%s' % (root, name, ext) thumb = '%s_%s%s' % (root, name, ext)