How do I serve an image from a Python Script?

Steve Holden sholden at holdenweb.com
Sat Jun 9 11:52:00 EDT 2001


Make sure you add the -u option to the #! line to run stdout in binary mode.

regards
 Steve

"haaserd" <haaserd at yahoo.com> wrote in message
news:3B2134A2.EFE1736F at yahoo.com...
> Hi,
>
> I am trying to serve an image from a Python script using
> Apache under
> Windows.  My simple HTML statement:
>
>     <img src="ShowPict.py?fn=xxx"  alt="a picture">
>
> ...and my simple script:
>
> #!c:/Python20/python.exe
> import sys
>
> def pix():
>     n = 'c:/My Pictures/Sample.jpg'
>     p = open(n,'rb')
>     apix = p.read()
>     p.close()
>     sys.stdout.write("content-type: image/jpeg\n\n")
>     sys.stdout.write(apix)
>     sys.stdout.flush()
>
> if __name__ == '__main__':
>     pix()
>
> As you can see I am ignoring the CGI fields for the moment
> and am just
> trying
> to serve up an image.  Through debugging statements that I
> have
> removed, I
> know the program crashes on the statement
>     sys.stdout.write(apix)
> if the picture is over 200,000 bytes.
>
> On small images (10k), the script runs to completion, but no
> image
> appears.
> What am I doing wrong?
>
> TIA,
>
> Roger Haase
>
>





More information about the Python-list mailing list