Here's what I do. Seems to work.

    Field('image', 'upload', uploadfolder=request.folder+'static/uploads',
            requires=IS_EMPTY_OR(IS_IMAGE())),
    Field('image_display', 'upload', 
uploadfolder=request.folder+'static/uploads',
            compute=lambda r: resize_image(r['image'], (320,320), 
'display'),
            readable=False, writable=False),
    Field('image_thumb', 'upload', 
uploadfolder=request.folder+'static/uploads',
            compute=lambda r: resize_image(r['image'], (150,130), 'thumb'),
            readable=False, writable=False),

def resize_image(image, size, path, rotate=0):
    import os.path
    from PIL import Image'
    if image:
        try:
            img = Image.open('%sstatic/uploads/%s' % (request.folder, 
image))
            img = img.convert("RGB")
            img.thumbnail(size, Image.ANTIALIAS)
            img = img.rotate(rotate)
            root, ext = os.path.splitext(image)
            filename = '%s_%s%s' %(root, path, ext)
            img.save('%sstatic/uploads/%s' % (request.folder, filename))
            return filename
        except Exception, e:
            return e
    else:
        return None

Reply via email to