/usr/share/pyshared/gluon/contrib/imageutils.py is in python-gluon 2.12.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # -*- coding: utf-8 -*-
#######################################################################
#
# Put this file in yourapp/modules/images.py
#
# Given the model
#
# db.define_table("table_name", Field("picture", "upload"),
# Field("thumbnail", "upload"))
#
# to resize the picture on upload
#
# from images import RESIZE
#
# db.table_name.picture.requires = RESIZE(200, 200)
#
# to store original image in picture and create a thumbnail
# in 'thumbnail' field
#
# from images import THUMB
# db.table_name.thumbnail.compute = lambda row: THUMB(row.picture, 200, 200)
#########################################################################
from gluon import current
class RESIZE(object):
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):
if isinstance(value, str) and len(value) == 0:
return (value, None)
from PIL import Image
import cStringIO
try:
img = Image.open(value.file)
img.thumbnail((self.nx, self.ny), Image.ANTIALIAS)
s = cStringIO.StringIO()
img.save(s, 'JPEG', quality=self.quality)
s.seek(0)
value.file = s
except:
return (value, self.error_message)
else:
return (value, None)
def THUMB(image, nx=120, ny=120, gae=False, name='thumb'):
if image:
if not gae:
request = current.request
from PIL import Image
import os
img = Image.open(os.path.join(request.folder, 'uploads', image))
img.thumbnail((nx, ny), Image.ANTIALIAS)
root, ext = os.path.splitext(image)
thumb = '%s_%s%s' % (root, name, ext)
img.save(request.folder + 'uploads/' + thumb)
return thumb
else:
return image
|