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


  • 0
    simpel  14 years ago
    You can do also:
    
    
    class RESIZE(object): 
        def __init__(self,nx=160,ny=80,error_message='niepoprawny plik'): 
            (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: 
                img = Image.open(value.file) 
                img.thumbnail((self.nx,self.ny), Image.ANTIALIAS) 
                s = cStringIO.StringIO() 
                img.save(s, 'JPEG', quality=100) 
                s.seek(0) 
                value.file = s 
            except: 
                return (value, self.error_message) 
            else: 
                return (value, None)
                
    def THUMB(image, nx=120, ny=120):
        from PIL import Image 
        import os  
        img = Image.open(request.folder + 'uploads/' + image)
        img.thumbnail((nx,ny), Image.ANTIALIAS) 
        root,ext = os.path.splitext(image)
        thumb='%s_thumb%s' %(root, ext)
        img.save(request.folder + 'uploads/' + thumb)
        return thumb
    
    
    db.define_table('gallery',
        Field('image', 'upload', required=True, notnull=True, requires=[IS_IMAGE(), RESIZE(650, 650)]),
        Field('image_thumb', 'upload', compute=lambda r: THUMB(r['image'])))
    
    
    
    Why can't I edit my own comment? Can somebody delete my first comment? Thanks

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