title: How to return in-memory PIL image from WSGI application
body: <p>I've read a lot of posts like <a href="https://stackoverflow.com/questions/1001068/creating-dynamic-images-with-wsgi-no-files-involved">this one</a> that detail how to dynamically return an image using WSGI. However, all the examples I've seen are opening an image in binary format, reading it and then returning that data (this works for me fine).</p>
  
  <p>I'm stuck trying to achieve the same thing using an in-memory PIL image object. I don't want to save the image to a file since I already have an image in-memory.</p>
  
  <p>Given this:</p>
  
  <pre><code>fd = open( aPath2Png, 'rb')
  base = Image.open(fd)
  ... lots more image processing on base happens ...
  </code></pre>
  
  <p>I've tried this:</p>
  
  <pre><code>data = base.tostring()
  response_headers = [('Content-type', 'image/png'), ('Content-length', len(data))]
  start_response(status, response_headers)
  return [data]
  </code></pre>
  
  <p>WSGI will return this to the client fine. But I will receive an error for the image saying there was something wrong with the image returned.</p>
  
  <p>What other ways are there?</p>