have somebody an example of image manipulation using PIL + web2py ??
i need to create different versions of an image when i upload it
(different sizes).
thanks a lot!
from os.path import getsize
import Image
def thumb(form):
try:
filename=form.vars.image.filename
zfile=form.vars.image_newfilename.split('.')
thumbnail=zfile[0] + '.' + zfile[1] + '.' + zfile[2] +
'.thumbnail.' + zfile[3]
ifile=request.folder + 'uploads/' + form.vars.image_newfilename
thumb = Image.open(ifile)
#print thumb.format, ' - ', thumb.size
fdim=thumb.size
fsz=getsize(ifile)
if fsz > 1024:
fsz=fsz/1024
if fsz > 1024:
fsz=round(fsz/1024.0,2)
fsz=str(fsz)+' MB'
else:
fsz=str(fsz)+' KB'
thumb.thumbnail((200,200), Image.ANTIALIAS)
ofile=request.folder + 'uploads/'+thumbnail
thumb.save(ofile)
if request.vars.id:
thumbnail0=db(db.foto.id==request.vars.id).select(db.foto.thumbnail)[0].thumbnail
if thumbnail0: unlink(request.folder + 'uploads/' + thumbnail0)
db(db.foto.id==request.vars.id).update(thumbnail=thumbnail,filename=filename,
filesize=fsz,
fileformat=thumb.format, filedim=str(fdim[0])+'x'+str(fdim[1]))
else:
db(db.foto.id==form.vars.id).update(thumbnail=thumbnail,filename=filename,
filesize=fsz,
fileformat=thumb.format, filedim=str(fdim[0])+'x'+str(fdim[1]))
except:
try:
if form.vars.image__delete== 'on':
thumbnail=db(db.foto.id==request.vars.id).select(db.foto.thumbnail)[0].thumbnail
unlink(request.folder + 'uploads/' + thumbnail)
db(db.foto.id==request.vars.id).update(thumbnail='',filename='')
except:
pass
-wes
> --
> You received this message because you are subscribed to the Google Groups "web2py-users" group.
> To post to this group, send email to web...@googlegroups.com.
> To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
>
>