CGI -- generating images -- \x0A always added!

Kevin other at cazabon.com
Wed Sep 10 01:13:32 EDT 2003


This works for me (save to a StringIO object first):

###############################

#!/usr/bin/python -u

# imports

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

# blah blah blah... prepare the image from the query string...

if os.path.isfile(filename):

    try:

        image = Image.open(filename)

        # save the image to stdout

        sfp = StringIO.StringIO()

        image.save(sfp,'jpeg')

    except Exception, errorMsg:

    print "Content-Type: text/html\n\n"

    print "Error occurred while processing Image (%s)<p>\n\n" %filename

    print "%s<p>\n%s" %(Exception, errorMsg)

    sys.exit(-1)

else:

    print "Content-Type: text/html\n\n"

    print "Error: requested image file does not exist: %s" %filename

    sys.exit(-1)

# if you got here, everything went ok, and the image is in spf, just send it
home

try:

    print 'Content-Type: image/jpeg\n' # Python adds the extra line feed to
separate the header

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

    sys.exit(0)

except Exception, errorMsg:

    # something went wrong in writing to sys.stdout?

    print "Content-Type: text/html\n\n"

    print "Error Printing Image and Header to sys.stdout\n<p>\n%s<p>\n%s"
%(Exception, errorMsg)

    sys.exit(-1)














More information about the Python-list mailing list