[Zope] PIL: can't call same method twice

Chris chris-zopemailing at gmx.de
Sun Mar 6 07:49:55 EST 2005


Hi David,

 > Yes! My suggestion would have made sense if image was passed by ref.
 > But then this *is* python.  If you can post ready-to-go code that
 > reproduces this I'd be curious to take a look if for no other reason

here is some code to reproduce. This is the striped down version of the 
relevant parts:
1. External Method (create an external method object 'my_thumbs' in ZMI
    pointing to the script on the fs
2. html-form (only for convenience - you can use a image file object
    instead)
3. the ZMI-Python script calling my_thumbs

I solved my problem now with a different "my_thumb" method returning a 
list of the needed thumb versions so I call Image.open() only once. But 
I am sure interested to find out what is causing the problem.

Regards, Chris

# external method for 'my_thumbs'
def thumbs(self, image, size=100):
   from PIL import Image
   image=Image.open(image) # this is the problem when called 2 times ...
   # do stuff with the image - not important for example
   image.thumbnail((size,size))
   file=StringIO()
   image.save(file, 'JPEG')
   return file.getvalue()

# html-form to provide an fileupload instance to 'resize_image'
<html><head><title>Add Image</title></head><body>
<form name="form" action="add_image" method="post"
       enctype="multipart/form-data">
   <input name="image" type="file">
   <input type="submit" value="submit Image">
</form>
</body></html>

# the resize_image ZMI Python script
image        = context.REQUEST.form['image']
image_small  = container.my_thumbs(image, size=100) # works :-)
image_medium = container.my_thumbs(image, size=200) # does not work :-(
# do stuff with the images (store, view, ...)
return 'Success :-)'




More information about the Zope mailing list