Returning a pil generated image from a python product
I am writing a product that returns an image generated by pil (unfortunately it doesn't): Create image- im = PIL.Image.new('RGB',(width,height)) it is then drawn- draw = ImageDraw.Draw(im) draw.rectangle([0,0,width,height], fill=(250,250,250)) etc etc etc after it is ready to be sent out I "save" it - pic = cStringIO.StringIO() im.save(pic, 'GIF') and send it pic.seek(0) RESPONSE.setHeader("Content-type", "image/gif") RESPONSE.write(pic.read()) my index_html <dtml-var standard_html_header> <IMG src="./dynamicImage?RESPONSE=RESPONSE"> <dtml-var standard_html_footer> the product class inherits Item, Persistent and Implicit the dynamicImage method has an argument RESPONSE When I click on view all I get is an image placeholder box (true as well when using the same image tag in a dtml method in the root folder), however from a dtml method in the root folder <dtml-var expr="TestObject.dynamicImage(RESPONSE)"> will render an image as the only content. any other content in the method is ignored. This is my first attempt at a product, so after much hair pulling I must get it to work. Thank you in advance for any help Sean K --------------------------------- Do you Yahoo!? Yahoo! Web Hosting - Let the expert host your site
Dive into this product's code to get to know how to return the image object from a PIL procedure. http://www.zope.org/Members/bowerymarc/ImageTag_Hotfix Ausum ----- Original Message ----- From: Sean K To: zope@zope.org Sent: Tuesday, November 19, 2002 5:43 PM Subject: [Zope] Returning a pil generated image from a python product I am writing a product that returns an image generated by pil (unfortunately it doesn't): Create image- im = PIL.Image.new('RGB',(width,height)) it is then drawn- draw = ImageDraw.Draw(im) draw.rectangle([0,0,width,height], fill=(250,250,250)) etc etc etc after it is ready to be sent out I "save" it - pic = cStringIO.StringIO() im.save(pic, 'GIF') and send it pic.seek(0) RESPONSE.setHeader("Content-type", "image/gif") RESPONSE.write(pic.read()) my index_html <dtml-var standard_html_header> <IMG src="./dynamicImage?RESPONSE=RESPONSE"> <dtml-var standard_html_footer> the product class inherits Item, Persistent and Implicit the dynamicImage method has an argument RESPONSE When I click on view all I get is an image placeholder box (true as well when using the same image tag in a dtml method in the root folder), however from a dtml method in the root folder <dtml-var expr="TestObject.dynamicImage(RESPONSE)"> will render an image as the only content. any other content in the method is ignored. This is my first attempt at a product, so after much hair pulling I must get it to work. Thank you in advance for any help Sean K Do you Yahoo!? Yahoo! Web Hosting - Let the expert host your site
From: "Sean K" <getmejazzed@yahoo.com>
pic = cStringIO.StringIO()
from cStringIO you can get the result as a string right? This could be worth a try: data = pic.getvalue() pic.close() RESPONSE.setHeader("Content-type", "image/gif") RESPONSE.setHeader('Content-Length', len(data)) return data It probably uses some more memory with big images, but unless you plan on MB size images it shouldn't be a problem.
<IMG src="./dynamicImage?RESPONSE=RESPONSE">
The RESPONSE=RESPONSE shouldn't really be needed, it should be passed anyway when called like that. Best Regards Lennart Regebro http://www.easypublisher.com/
Hi, --On Dienstag, 19. November 2002 14:43 -0800 Sean K <getmejazzed@yahoo.com> wrote:
I am writing a product that returns an image generated by pil (unfortunately it doesn't):
Create image-
im = PIL.Image.new('RGB',(width,height))
it is then drawn-
draw = ImageDraw.Draw(im)
draw.rectangle([0,0,width,height], fill=(250,250,250)) etc etc etc
after it is ready to be sent out I "save" it -
pic = cStringIO.StringIO() im.save(pic, 'GIF')
and send it
pic.seek(0) RESPONSE.setHeader("Content-type", "image/gif") RESPONSE.write(pic.read())
You dont need the step with StringIO - you see your RESPONSE object has a write() method too. In fact it acts like a open (for writing) file object. If it does not work - did you think of right permissions? And did you give a docstring to the index_html/__str__/__call__ method(s)? Regards Tino
Thanks for the responses, I have tinkered and am able to get the image out to the browser via the index_html of the product. When I try and access an instance from <dtm-var picture>(for example) in a dtml method however, the image doesn't make it to the browser while all the other content in the method does. Simplified code - product still inherits as bellow in original post. def pict(self): im=PIL.Image.new('RGB', (200, 200)) draw = PIL.ImageDraw.Draw(im) draw.rectangle([0,0,0,0], fill =(0,0,0)) RESPONSE.setHeader("Content-type", "image/gif") outFile = cStringIO.StringIO() im.save(outFile, 'gif') return outFile.getvalue() complete index_html - <IMG src="../pict"> (contained in a subfolder of the product called dtml) I am guessing that the image is maybe destroyed before it makes it to the dtml method but am probobly completely wrong. Once again, thankyou in advance for your help Sean Kemplay Sean K <getmejazzed@yahoo.com> wrote: I am writing a product that returns an image generated by pil (unfortunately it doesn't): Create image- im = PIL.Image.new('RGB',(width,height)) it is then drawn- draw = ImageDraw.Draw(im) draw.rectangle([0,0,width,height], fill=(250,250,250)) etc etc etc after it is ready to be sent out I "save" it - pic = cStringIO.StringIO() im.save(pic, 'GIF') and send it pic.seek(0) RESPONSE.setHeader("Content-type", "image/gif") RESPONSE.write(pic.read()) my index_html <dtml-var standard_html_header> <IMG src="./dynamicImage?RESPONSE=RESPONSE"> <dtml-var standard_html_footer> the product class inherits Item, Persistent and Implicit the dynamicImage method has an argument RESPONSE When I click on view all I get is an image placeholder box (true as well when using the same image tag in a dtml method in the root folder), however from a dtml method in the root folder <dtml-var expr="TestObject.dynamicImage(RESPONSE)"> will render an image as the only content. any other content in the method is ignored. This is my first attempt at a product, so after much hair pulling I must get it to work. Thank you in advance for any help Sean K --------------------------------- Do you Yahoo!? Yahoo! Web Hosting - Let the expert host your site --------------------------------- Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now
participants (4)
-
Ausum Studio -
Lennart Regebro -
Sean K -
Tino Wildenhain