[Zope] Resizing uploaded image in external method with Pil

Anja DeRidder sebrechts.deridder@pandora.be
Sat, 08 Jul 2000 23:20:30 +0200


Resizing uploaded image in external method with Pil

Peter Sebrechts     sebrechts.deridder@pandora.be


Hello,

I have a Form, where I can select an Image to upload, combined with
several fill-in fields for the Image-properties.

The Form starts with:
<form action="add_newImage_html" METHOD="POST"
ENCTYPE="multipart/form-data">

The uploadfield is:
<input type="file" name="newimage" value="">

Next are the other input-fields and the Submit button.
--------------------

The uploadform and the DTMLmethod 'add_newImage_html' reside in the
folder 'Images', where the new created image-objects are stored.
In each image-object is the uploaded newimage stored, and also a
subfolder,  where a new created thumbnail from the original image is
placed.
The resizing to a thumbnail is done in an external method with Pil.
And here is my problem:
I can pass an Image trough an external method without resizing it.
However,  when I try to open it with " im = Pil.Image.open(newimage)",
I receive an errormessage: 'cannot identify image file'
After long searches in the Archives, I'm aware that it has something to
do with making the image-data available as a string or as a file-handle.
After many trials,  I've got only errors...

The dtml-method 'add_newImage_html' is as follows:

<dtml-with REQUEST>
   <dtml-if "newimage.filename">
      <dtml-call  "REQUEST.set('IDnr',REQUEST.form['new_ IDnr '])">
      <dtml-with "manage_addProduct['ImageProduct']">
         <dtml-call "Imageclass_add(_.None,_, NoRedir=1)">
      </dtml-with>
      <dtml-in "objectValues(['Images'])">
         <dtml-if "(_['sequence-item'].id == REQUEST.form['new_ IDnr
'])">
            <dtml-call "REQUEST.set('new_ArtNo',_['sequence-item'])">
         </dtml-if>
      </dtml-in>
      <dtml-call "_['new_ IDnr
'].manage_addImage(newimage.filename,newimage)">
      <dtml-call "_['new_ IDnr '].manage_addFolder('preview')">
# until now, everything is OK:  image-object is created, image stored
and previewfolder created.

#now, I try to make a thumbnail and store it in the 'preview' folder:
      <dtml-with "_['new_ IDnr '].preview">
          <dtml-call
"manage_addImage(newimage.filename,thumbmaker(newimage))">
      </dtml-with>

--------------------
The external method 'thumbmaker':

def  thumbmaker (self,newimage):
 """convert image to preview-size (128 x 128) """
 import Pil
 im = Pil.Image.open(newimage)
 im = im.thumbnail((128,128))
 return im

I tried also:
 im = Pil.Image.open(StringIO(newimage))     # (with StringIO imported)

and in the dtml-method:
<dtml-call
"manage_addImage(newimage.filename,thumbmaker(str(newimage)))">

and passing via REQUEST.newimage from the dtml- to the external method

and finally refering to the already stored image was also without
result.


Ps:  I know that Photo does something similar,  but it seems me not easy
to combine with my multi-inputfield uploadForm.

Peter