[Image-SIG] StringIO seems to garble images in cgi

Fredrik Lundh fredrik@pythonware.com
Sat, 7 Dec 2002 19:19:56 +0100


Doug.Shawhan@gecits.ge.com wrote:

> The following almost works, unfortunately the image comes out garbled. I
> have tried a variety of images in case they are somehow corrupt- no dice. Am
> I missing a step?

try saving an image to disk from the browser, and also from within
the CGI, and compare the files.  if the sizes differ, chances are that
your server is ignoring the "-u" option on the first line.

</F>

> #!c:/python22/python.exe -u 
> 
> import cgi, StringIO
> #import cgitb; cgitb.enable()
> import sys
> import Image
> 
> 
> image=Image.open("pics\\karate2.jpg")
> image.thumbnail((100,100))
> image_tub = StringIO.StringIO()
> image.save(image_tub,'JPEG')
> 
> try:
>     print 'Content-Type: image/jpeg\n' 
>     sys.stdout.write(image_tub.getvalue())
>     sys.exit(0)
> except Exception, errorMsg:
>     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)