returning a picture from PIL

Joonas Paalasmaa joonas at olen.to
Tue Mar 12 15:25:12 EST 2002


Peter Posselt Vestergaard wrote:
> 
> Hi
> I would like to show a image on a webpage by calling a python script that
> through the Python Imaging Library (PIL) loads a picture and then returns
> it.
> I'm trying to do it like this:
> In the webpage I write:
> [...]
> <img src="http://www.myserver/myscript.py" height="400" width"=500>
> [...]
> 
> and in myscript.py I write:
> 
> [...]
> pic = Image.open(urllib.urlretrieve(url)[0])
> print "Content-type: image/png\n\n"
> pic.save(sys.stdout,"PNG")
> 
> the page takes some time loading the picture but nothing is ever shown. If I
> change sys.stdout to "filename" the picture is stored perfectly on my disk.
> Anyone who can tell me how to do this kind of output from PIL?

If you're using Windows, the problem might be the "\n" -> "\r\n"
conversion.
To avoid the conversion, you have to invoke python in unbuffered mode
with -u option or use msvcrt module.
Take a look at an useful Python Cookbook recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443



More information about the Python-list mailing list