Matthew R
Matthew R

Reputation: 232

Serving Dynamic Images In Python 3.3

I am trying to use python (version 3.3) with PIL to generate dynamic images for a web page. I have verified that the contents of my generated image are fine with Image.show() but I can not get the output formatted so that a browser displays it correctly.

My current code results in the browser doing nothing at all (not reporting an error or navigating away from the current page), however terminal output appears to be correct to me.

sys.stdout.write("Content-type: image/png\r\n\r\n")
img.save(sys.stdout.buffer, "PNG")

I have also tried using an io.BytesIO() object, writing to that then printing it, but this produces the same results and the terminal output appears to be identical.

I'm out of ideas as to where the error is, any help is greatly appreciated.

Upvotes: 2

Views: 1325

Answers (1)

Brent Washburne
Brent Washburne

Reputation: 13158

It looks like this is a duplicate question:

Correct way to serve a dynamic image in Python/Web.Py using PIL

Also, remember to include the status code before the Content-type:

sys.stdout.write('Status: 200 OK\r\n')

Upvotes: 0

Related Questions