its better to keep the TemporaryFile so that the image filesize gets 
updated, otherwise the image filesize will be the same as the original and 
it should be smaller

On Thursday, September 2, 2010 12:30:49 PM UTC+1, kachna wrote:
>
> One more update. "tempfile.TemporaryFile()" in not needed. 
>
>         def __call__(self, value): 
>
>                 import Image 
>                 import cgi 
>
>                 try: 
>                         im = Image.open(value.file) 
>                         print im.size, im.format 
>
>                         im.thumbnail((100, 100), Image.ANTIALIAS) 
>
>                         value.file.seek(0) 
>                         im.save(value.file, im.format) 
>                         value.file.seek(0) 
>
>                         return (value, None) 
>                 except Exception as e: 
>                         print 'resize exception:', e 
>                         return (value, self.error_message) 
>
>
> On 2 zář, 12:34, kachna <[email protected]> wrote: 
> > Thanks for hint. 
> > Here is my working solution. It takes lot of time. 
> > 
> >         def __call__(self, value): 
> > 
> >                 import Image 
> >                 import cgi 
> >                 import tempfile 
> > 
> >                 try: 
> >                         im = Image.open(value.file) 
> >                         print im.size, im.format 
> > 
> >                         im.thumbnail((100, 100), Image.ANTIALIAS) 
> > 
> >                         resized = tempfile.TemporaryFile() 
> > 
> >                         im.save(resized, im.format) 
> > 
> >                         value.file = resized 
> >                         value.file.seek(0) 
> > 
> >                         return (value, None) 
> >                 except Exception as e: 
> >                         print 'resize exception:', e 
> >                         return (value, self.error_message) 
> > 
> > - In-value in __call__ is just one Field of cgi.FieldStorage. Here is 
> > upload filed. In value.file is open file with uploaded data. 
> > - Final image is stored in  tempfile.TemporaryFile(). It must be 
> > rewind before return! 
> > - __call__ return Field of cgi.FieldStorage with new value.file 
> > 
> > On 1 zář, 23:25, mdipierro <[email protected]> wrote: 
> > 
> > 
> > 
> > > This is a bit more complex than it seems because upload expects a 
> > > ci.FieldStorage object so the validator __call__ should return a 
> > > cgi.FieldStoarge. 
> > 
> > > On Sep 1, 3:50 pm, kachna <[email protected]> wrote: 
> > 
> > > > Hi, 
> > > > I am trying to write validator to resize my image before storing in 
> DB 
> > > > field. 
> > 
> > > > class RESIZE_IMG(object): 
> > > >         def __init__(self, isThumb=False, 
> error_message='unresizable'): 
> > > >                 (self.isThumb, self.error_message) = (isThumb, 
> error_message) 
> > 
> > > >         def __call__(self, value): 
> > > >                 import Image 
> > 
> > > >                 try: 
> > 
> > > >                         im = Image.open(value.file) 
> > > >                         # This print is OK. PIL recognize uploaded 
> image 
> > > >                         print im.size, im.format 
> > 
> > > >                         im.resize((100, 100)) 
> > > >                         value.file.seek(0) 
> > > >                         im.save(value.file, im.format) 
> > > >                         return (value, None) 
> > > >                 except Exception as e: 
> > > >                         print 'resize exception:', e 
> > > >                         return (value, self.error_message) 
> > 
> > > > Script runs well but data in DB field is no image data. It can' t be 
> > > > displayed by download function from default controller too. 
> > 
> > > > Thanks for help.

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to