[Zope] Displaying PIL Images in Zope Templates

Nico Grubert nicogrubert at gmail.com
Thu Sep 18 03:48:19 EDT 2008


> You can't do this....
> 
> What you need to do is associate the image with a URL so it gets
> generated when the browser processes the <img> tag.. UNLESS it's very small
> in which case you can use the src="..." attribute to contain the data.
> 
> So a PIL image should be returned by a script or external method that
> you can browse to. Headers such as Content-Type should be set to set the
> image type.
> 
> Instead of generating the image on the call to your ZPT you form an <img
> src="/some/url/that/makes/my/image?foo=bar&baz=bat"> e.g. in order to
> generate the right image.

Hi Andrew

thank you for your answer.
I tried this but calling the page template only shows the image and the 
other content is not shown. I guess, it has something to do with the 
REQUEST.RESPONSE.setHeader('Content-Type', WEB_FORMAT) I set in the 
external method.

Here is the code I use:

External method "createThumbnail":
----------------------------------
def createThumbnail(self, photo):
     """ create PIL image
     """
     <snip>
     thumb_data = StringIO()
     # 'thumb' is a PIL.Image.Image instance
     thumb.save(thumb_data, 'JPEG')

     REQUEST = self.REQUEST
     REQUEST.RESPONSE.setHeader('Content-Type', 'JPEG')
     return REQUEST.RESPONSE.write(thumb_data.getvalue())


Page Template "thumbnails.html":
--------------------------------
<h1>Thumbnails</h1>
<p>some other content...</p>
<tal:rep tal:repeat="photo photos">
  <a tal:attributes="href photo/absolute_url;">
    <img tal:attributes="src python: container.createThumbnail(photo)" />
  </a>
</tal:rep>
<p>some other content...</p>


In the page templates I iterate over a list of image objects ("photos") 
and pass a single image object to external method.
As I mentioned above, this only shows the first image and not the other 
content nor the other images.

Did I miss something?

Regards
Nico


More information about the Zope mailing list