[Zope] PIL Question

foohbah zope@zope.org
Sun, 12 May 2002 12:08:47 -0400


Ah. I see.
To solve this, I made this very simple fakefile object:

class FakeFile:
     '''
     memory map equivalent of a real file
     '''
     def __init__(self):
         self.contents = []

     def write(self,s):
         self.contents.append(s)

     def read(self):
      return string.join(self.contents,'')


and I tell PIL to write to it - the write method takes any file like 
object - as long as it has a write method. The code (where ldp is a PIL 
canvas) sort of looks like:

     fakef = FakeFile()
     ldp.writeimage(fakef,'jpeg')

I then return fakef.read() from my external method as the binary image.

Does that help?

When you've finished, please write this up as a howto if you have time?

cheers..

J. Joy wrote:

> 
> 
>   Ross Lazarus <do_not_reply_to_this@bellatlantic.net> wrote:
> 
>     the img tag expects the server to deliver an existing image file from
>     somewhere. What you need to do is a little more complex (!). You COULD
>     write the image into a zope object (bad idea for lots of reasons - eg
>     zodb bloat) or to a localFS (better but still sucky) and deliver it
>     from
>     there using an img tag. AFAIK, this is the ONLY way to embed your image
>     into a page with text and stuff.
> 
> This is not a problem.  In the HTML example of how this is called, I may 
> not have shown
> clearly enough that what I want to do is have the src be an object in 
> the same folder that
> would generate this image upon calling it and the external python method 
> would generate
> and return the image back.  The part that I am having trouble with is 
> with the return part of
> my external method.  I do not know how to make it return the data of the 
> image that was just
> generated.  I can save it to a file, but I can't find a nice way of 
> returning the image data back
> from the call.  I searched for a few hours to try to find some practical 
> examples of perhaps
> how to do this on google, but nothing came up. 
> 
> To wit, the External Call should act like a image when referenced, it's 
> just having "stuff" passed to
> it to form the image in question. :)
> 
> The External Call would be called like:
> 
> <img src="MakeImage">
> 
> Where MakeImage would be the External Method.
> 
> 
>  >
>  > I'm trying to create an external method that will take variables that 
> exist in a ZSQL call and will output an image
>  >
>  > based on that:
>  >
>  > import Image, ImageDraw, StringIO, os.path
>  >
>  > def PILtest():
>  > image = Image.new('RGB',[25,25])
>  > ## IMage Stuff Happens here!
>  > return image
>  >
>  > Where this would occur in a DTML-Method of:
>  >
>  >
>  > <http://us.f149.mail.yahoo.com/ym/PILtest>
>  >
>  >
>  > But I can't get PILtest to output the image itself. Save() doesn't 
> seem to be of help directly. Anyone have any suggestions?
>  >