Newbie has question that's not exactly Python...

Gary Walker borealis3 at home.com
Tue Apr 3 17:08:31 EDT 2001


Chris,

Your answer *looks* like it might work, but, unfortunately, it doesn't.  Let
me quickly say that I realize that you were typing it off the top of your
head, and so one really shouldn't expect it to run.  But to me, it looks
rather complete... but I think something's missing...

Here's some actual code of mine that doesn't work:

************************************
#!/usr/bin/python

import StringIO, cgi, os, sys, string, Image

im = Image.open('backgroundimage.gif')
# here I'll be drawing all over my image...

# Now I want to send it to a browser...
sfp = StringIO.StringIO()
im.save(sfp,'gif')

print 'Content-Type: image/gif\r\n\r\n'
sys.stdout.write(sfp.getvalue())
************************************

Do you (or anyone else please??) see what's wrong here? It seems like it
oughta work.

As you can see, I've incorporated most of Chris's solution (see below), but
I'm not a savvy enough Python programmer to know what's missing. I'd love to
know the "right" way of doing this, so I can get on with the next piece of
my program...

Can anyone suggest a working solution?

Thanks!!

Gary Walker

Chris Gonnerman wrote in message ...
>You could use the Python Imaging Library.  I'm no expert on it; I use
>gdmodule,
>but PIL is more "standard."  You would do something like this in a cgi:
>
>    import sys
>    from PIL import Image
>    from StringIO import StringIO
>
>    img = Image.open("background.gif")  # or Image.new(...) for a blank
>image
>    # ... do some drawing on the image ...
>    sfp = StringIO()
>    img.save(sfp, "gif")  # possibly with keyword options
>    sys.stdout.write(sfp.getvalue())
>
>The StringIO is required as the save() method of the Image object requires
>seek() and tell() methods, so we can't just say
>
>    img.save(sys.stdout, "gif")  # BIG NO NO
>
>because sys.stdout may be attached to a pipe or socket.
>
>As I said, I'm no expert on PIL, so your mileage may vary.  There may be an
>easier way to do this... anyone else care to comment?
>






More information about the Python-list mailing list