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 <mdipie...@cs.depaul.edu> 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 <petr.marti...@gmail.com> 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.

Reply via email to