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

Rob Myroon robmyroon@yahoo.com
Mon, 9 Dec 2002 12:33:12 -0800 (PST)


I am not sure if this helps but I had to do a seek(0) when I used StringIO the other day.

      fp = StringIO.StringIO()        # create StringIO object
      fp.write(string)                # write data into StringIO object
      fp.seek(0)                      # put file pointer to the beginning
      im = Image.open(fp)             # create a PIL Image object

Rob

--- 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?
> 
> d
> 
> ---------------------snip----------------------------------------------
> 
> 
> #!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)
> 
> 
> 
> 
> _______________________________________________
> Image-SIG maillist  -  Image-SIG@python.org
> http://mail.python.org/mailman/listinfo/image-sig


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com