title: How to save image in-memory and upload using PIL?
body: <p>I'm fairly new to Python. Currently I'm making a prototype that takes an image, creates a thumbnail out of it and and uploads it to the ftp server.</p>
  
  <p>So far I got the get image, convert and resize part ready.</p>
  
  <p>The problem I run into is that using the PIL (pillow) Image library converts the image is a different type than that can be used when uploading using storebinary()</p>
  
  <p>I already tried some approaches like using StringIO or BufferIO to save the image in-memory. But I'm getting errors all the time. Sometimes the image does get uploaded but the file appears to be empty (0 bytes).</p>
  
  <p>Here is the code I'm working with:</p>
  
  <pre><code>import os
  import io
  import StringIO
  import rawpy
  import imageio
  import Image
  import ftplib
  
  # connection part is working
  ftp = ftplib.FTP('bananas.com')
  ftp.login(user="banana", passwd="bananas")
  ftp.cwd("/public_html/upload")
  
  def convert_raw():
      files = os.listdir("/home/pi/Desktop/photos")
  
      for file in files:
          if file.endswith(".NEF") or file.endswith(".CR2"):
              raw = rawpy.imread(file)
              rgb = raw.postprocess()
              im = Image.fromarray(rgb)
              size = 1000, 1000
              im.thumbnail(size)
  
              ftp.storbinary('STOR Obama.jpg', img)
              temp.close()
      ftp.quit()
  
  convert_raw()
  </code></pre>
  
  <p>What I tried:</p>
  
  <pre><code>temp = StringIO.StringIO
  im.save(temp, format="png")
  img = im.tostring()
  temp.seek(0)
  imgObj = temp.getvalue()
  </code></pre>
  
  <p>The error I'm getting lies on the line <code>ftp.storbinary('STOR Obama.jpg', img)</code>.</p>
  
  <p>Message:</p>
  
  <pre class="lang-none prettyprint-override"><code>buf = fp.read(blocksize)
  attributeError: 'str' object has no attribute read
  </code></pre>