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


  • 0
    rppowell  14 years ago
    just wanted to add that the thumbnail image name matters (at least in the current version of web2py). I ended up with this code:
    
    def makeThumbnail(img_name,size=(150,150)):
        try:    
            import os
            from PIL import Image
        except: 
            return
        im=Image.open(request.folder + 'uploads/' + img_name)
        im.thumbnail(size,Image.ANTIALIAS)
        root,ext = os.path.splitext(img_name)
        thumbName='%s_thumb%s' %(root, ext)
        im.save(request.folder + 'uploads/' + thumbName)
        return thumbName
    
    def add():
        form = SQLFORM(db.Item)
        if form.accepts(request.vars, session):
            response.flash = "Item Added"
            row = db(db.Item.id==form.vars.id).select()[0]
            img_name=row.image
            thumb = makeThumbnail(img_name)
            if thumb: row.update_record(image_thumb=thumb)
        elif form.errors:
            response.flash = "Form has errors"
        return dict(form=form)
    
    Does the thumbnail really need a UUID that is not related to the original image?

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