On Feb 18, 2:56 pm, Luther Goh Lu Feng <[email protected]> wrote:
> I have many images that are already uploaded. I would like to have a
> way to resize these images proportionately in the view. The original
> images may or may not be modified. Any ideas how this can be achieved?
def THUMB(image, nx=120, ny=120,
basefolder=os.path.join(request.folder, 'uploads')):
'''Utility for making thumbnails. Will be used inside a DB
compute function
http://web2pyslices.com/main/slices/take_slice/62 '''
import Image
img = Image.open(os.path.join(basefolder, image))
img.thumbnail((nx,ny), Image.ANTIALIAS)
root, ext = os.path.splitext(image)
thumb ='%s_thumb%s' % (root, ext)
img.save(os.path.join(basefolder, thumb))
return thumb
dbh.define_table(
'table_name',
Field('image', 'upload', uploadfolder=uploadfolder,
autodelete=True),
Field('thumbnail', 'upload', uploadfolder=uploadfolder,
autodelete=True,
compute = lambda r: THUMB(r['image'],
basefolder=uploadfolder)),
)
This is almost copy-paste from one of my apps. Works like a charm.
Note the reference to the slice.