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

Chris Gonnerman chris.gonnerman at usa.net
Tue Apr 3 23:45:35 EDT 2001


Ok, here is a working block of code based on yours:

#!/usr/bin/env 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\n'

sys.stdout.write(sfp.getvalue())

#---------------------------------------------------------

Andrew was right on the money regarding the print statement
and the extra \n it adds.  I generally do it like this,
though, *depending* on that extra newline.  On my Apache
server under Python 1.5.2, this works great.  I didn't need
the \r's either.

Incidentally, here is a suggestion:

#!/usr/bin/env python
#---------------------------------------------------------

import sys

sys.stderr = sys.stdout
sys.stdin.close()

try:
    import StringIO, cgi, os, 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\n'

    sys.stdout.write(sfp.getvalue())

except:
    print 'Content-Type: text/plain\n'
    import traceback
    traceback.print_exc(file=sys.stdout)

#---------------------------------------------------------

If you have problems, you just point your browser directly
at the CGI script and you get readable error messages!






More information about the Python-list mailing list