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




def makeThumbnail(dbtable,ImageID,size=(150,150)):
        import os, uuid
        from PIL import Image
    except: return
    im=Image.open(request.folder + 'uploads/' + thisImage.mainfile)
    thumbName='uploads.thumb.%s.jpg' % (uuid.uuid4())
    im.save(request.folder + 'uploads/' + thumbName,'jpeg')

def uploadimage():
    dbtable = db.uploads          #uploads table name
    if len(request.args):
        records = db(dbtable.id==request.args[0]).select()
    if len(request.args) and len(records):
        form = SQLFORM(dbtable, records[0], deletable=True)
        form = SQLFORM(dbtable)
    if form.accepts(request.vars, session): 
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    ## Quick list just to demonstrate...
    list = crud.select(dbtable)
    return dict(form=form,list=list)

(I am grateful to Lukasz (aka ls1) for his kind assistance).

Related slices

Comments (11)

  • Login to post

  • 0
    gheither 10 years ago

    This slice used to work for me about a year ago, now it's not after I upgraded my web2py.  Can anyone get this working with the latest web2py build?

    It creates the thumbnail successfully and stores it in the uploads folder, but when you try to retrieve the thumbnail through the URL bar or the DB admin screen you get a ticket.

    <type 'exceptions.TypeError'> Can't retrieve uploads.thumb.16e0d9fd-bc66-4b36-9c75-03aaea1d8603.PNG

    Please help!

  • 0
    eanappi 11 years ago
    I did what I explained and it worked perfect. But when uploading in Amazon EC2 does not work.
    There are dependencies to use this module?
    replies (1)
    • villas 11 years ago

      Of course, as noted you need PIL to be installed. Check filesystem permissions of the folder that your python/webserver can write files. BTW I like the code by simpel in the comments.

  • 0
    write2ariya 12 years ago

    Dear David


    This is very useful. I use you code in my project. Thank you for your contribution & sharing.

    (just wanna thanks, I will payback web2py community someway)


  • 0
    simpel 13 years ago
    You can do also:
    class RESIZE(object): 
        def __init__(self,nx=160,ny=80,error_message='niepoprawny plik'): 
        def __call__(self,value):
            if isinstance(value, str) and len(value)==0: 
                return (value,None) 
            from PIL import Image 
            import cStringIO 
                img = Image.open(value.file) 
                img.thumbnail((self.nx,self.ny), Image.ANTIALIAS) 
                s = cStringIO.StringIO() 
                img.save(s, 'JPEG', quality=100) 
                value.file = s 
                return (value, self.error_message) 
                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
        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

  • 0
    mrfreeze 13 years ago
    I deleted it and will add comment editing. I thought previewing would be enough.
show more comments

Hosting graciously provided by:
Python Anywhere