Re: [Zope] Displaying PIL Images in Zope Templates
The <span> renders "<PIL.Image.Image instance at 0x23b97500>". How can I show the image?
Call its tag method.
<span tal:replace="structure item/tag" />
Hi Paul, how is the susi going? ;-) I tried <span tal:replace="structure item/tag" /> but I get the following error: ----------------------------------------------------------------------- Exception Type TraversalError Exception Value (<PIL.Image.Image instance at 0x18e84ad0>, 'tag') * Module Products.PageTemplates.Expressions, line 153, in _eval * Module zope.tales.expressions, line 124, in _eval * Module Products.PageTemplates.Expressions, line 83, in boboAwareZopeTraverse * Module zope.traversing.adapters, line 164, in traversePathElement __traceback_info__: (<PIL.Image.Image instance at 0x18e84ad0>, 'tag') * Module zope.traversing.adapters, line 52, in traverse __traceback_info__: (<PIL.Image.Image instance at 0x18e84ad0>, 'tag', []) TraversalError: (<PIL.Image.Image instance at 0x18e84ad0>, 'tag') ----------------------------------------------------------------------- However, I could not find any "tag" attribute in the lists of available attributes of the PIL.Image.Image instance. dir(<PIL_object>) returns: ['_Image__transformer', '__doc__', '__getattr__', '__init__', '__module__', '_copy', '_dump', '_expand', '_makeself', '_new', 'category', 'convert', 'copy', 'crop', 'draft', 'encoderconfig', 'encoderinfo', 'filter', 'format', 'format_description', 'fromstring', 'getbands', 'getbbox', 'getcolors', 'getdata', 'getextrema', 'getim', 'getpalette', 'getpixel', 'getprojection', 'histogram', 'im', 'info', 'load', 'mode', 'offset', 'palette', 'paste', 'point', 'putalpha', 'putdata', 'putpalette', 'putpixel', 'quantize', 'readonly', 'resize', 'rotate', 'save', 'seek', 'show', 'size', 'split', 'tell', 'thumbnail', 'tobitmap', 'tostring', 'transform', 'transpose', 'verify'] No "tag" method there... :-( Regards Nico
+-------[ Nico Grubert ]---------------------- | > > The <span> renders "<PIL.Image.Image instance at 0x23b97500>". | > > How can I show the image? | | > Call its tag method. | | > <span tal:replace="structure item/tag" /> | | | Hi Paul, | | how is the susi going? ;-) | | I tried <span tal:replace="structure item/tag" /> but I get the | following error: 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. -- Andrew Milton akm@theinternet.com.au
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
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.
Got it. The magic is not to call the external method by container.createThumbnail() but use absolute URL to the external method. In the page template I have to use: <img tal:attributes="src string:${container/absolute_url}/createThumbnail?imagepath=${imgpath}"/> Thanks for pointing me in the right direction. Regards Nico
On Thu, Sep 18, 2008 at 08:22:37AM +0200, Nico Grubert wrote:
The <span> renders "<PIL.Image.Image instance at 0x23b97500>". How can I show the image?
Call its tag method.
<span tal:replace="structure item/tag" />
Hi Paul,
how is the susi going? ;-)
Ah, I sold mine years ago. Nowadays I mostly just play bass. And you? :)
I tried <span tal:replace="structure item/tag" /> but I get the following error: ----------------------------------------------------------------------- Exception Type TraversalError Exception Value (<PIL.Image.Image instance at 0x18e84ad0>, 'tag')
Whoops, sorry, I didn't realize these were PIL images, I thought they were OFS.Image or some such. But I see you already got the solution. - PW -- Paul Winkler http://www.slinkp.com
participants (3)
-
Andrew Milton -
Nico Grubert -
Paul Winkler