title: How to create png image file-object from numpy array in python without saving to disk (for http request)
body: <p>I need to submit PNG-images to a blackbox server with a http request. I use python3 to generate images in a numpy 64x64x3 array. What I currently do is:</p>
  
  <ol>
  <li>Generate image</li>
  <li>Save image with scipy.misc.toimage to disk</li>
  <li>Open saved image file from disk</li>
  <li>Use requests module to send http requests with the image opened image file object in it</li>
  </ol>
  
  <p>This works perfectly fine, but I want to get rid of step 2 and 3, so I do not need to save my object to disk first and then load it again. Instead I would like to convert my numpy array in a file-object that is compatible with the http server and send it directly. (Like one you get from open() )</p>
  
  <p>I know it is easy to convert from numpy array to PNG-image with PIL for example, but I only find how to do that combined with saving to disk in one function.</p>
  
  <p>Thank you very much for any help!</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>import numpy as np
  import requests
  from scipy.misc import toimage
  
  arr = generate64x64x3ImageWithNumpy()
  toimage(arr, cmin=0.0, cmax=255.0).save('tmp.png')
  d = {'key':API_KEY}
  f= {'image': open('tmp.png', 'rb')}
  result = requests.post(SERVER_URL, files=f, data=d)
  </code></pre>
  
  <p>I want this:</p>
  
  <pre><code>arr = generate64x64x3ImageWithNumpy()
  
  not_on_disk = numpyArrayToPNGImageWithoutSavingOnDisk(arr)
  
  d = {'key':API_KEY}
  f = {'image': not_on_disk}
  result = requests.post(SERVER_URL, files=f, data=d)
  </code></pre>