If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!


  • 0
    sherdim  14 years ago
    This solution supposes initial keeping of the uploaded BIG image. I am not a specialist in the webserver memory utilization, but a solution with validator RESIZE, which called on any upload to the given field is more straightforward IMHO. Such solution was published in the web2py group about a year ago. It utilizes PIL also and cStringIO as a temporal buffer. The same approach was for automarking images with the site logo. Place a module with validator in module folder. ================ __all__ = [ 'RESIZE', ] class RESIZE(object): """ Resize image such as thumbnail """ def __init__(self,nx=160,ny=80,error_message='image resize error'): (self.nx,self.ny,self.error_message)=(nx,ny,error_message) def __call__(self,value): if isinstance(value, str) and len(value)==0: return (value,None) from PIL import Image import cStringIO try: #load im=Image.open(value.file) #resize im.thumbnail((self.nx,self.ny),Image.ANTIALIAS) #by hand s=cStringIO.StringIO() im.save(s,'JPEG',quality=86) s.seek(0) value.file=s except: return (value,self.error_message) else: return (value,None) =========== Vale!

Commented on:

This is a method I found to upload an image (into the uploads folder) and then create the thumbnail in the same directory. One good point is that Web2py allocates the filenames so everything can have authorization etc. It requires the Python Image Library installed.

Hosting graciously provided by:
Python Anywhere