title: How do I post an Image object with a multi-part POST with Request?
body: <p>I'm trying to convert some <code>javascript</code> code I wrote to <code>Python</code>, but am getting stuck on passing data b/t <code>PIL</code> and <code>Requests</code> objects.</p>
  
  <p>My <code>python</code> script downloads an image to memory:</p>
  
  <pre><code>from PIL import Image
  import urllib2
  import cStringIO
  
  def fetch_image_to_memory(url):
     req = urllib2.Request(url, headers={
                           'User-Agent': "Mozilla / 5.0 (X11; U; Linux i686) Gecko / 20071127 Firefox / 2.0.0.11"})
     con = urllib2.urlopen(req)
     imgData = con.read()
     return Image.open(cStringIO.StringIO(imgData))
  </code></pre>
  
  <p>I'd then like to add it to the <code>form data</code> for a <code>POST</code> operation.  This code succeeds when the file is on disk:</p>
  
  <pre><code>from requests_toolbelt import MultipartEncoder
  import requests
  url = 'https://us-west-2.api.scaphold.io/graphql/some-gql-endpoint'
  
  multipart_data = MultipartEncoder(
      fields={
           'query':'some-graphql-specific-query-string',
          'variables': '{ "input": {"blobFieldName": "myBlobField" }}',
  
          ## `variables.input.blobFieldName` must hold name 
          ## of Form field w/ the file to be uploaded
          'type': 'application/json',
          'myBlobField': ('example.jpg', img, 'image/jpeg')
      }
  )
  req_headers = {'Content-Type':multipart_data.content_type, 
               'Authorization':'Bearer secret-bearer-token'}
  r = requests.post(url, data=multipart_data, headers=req_headers)
  </code></pre>
  
  <p>However, when trying to pass in an <code>Image</code> object from the <code>fetch_image_to_memory</code> function:</p>
  
  <pre><code> 'myBlobField': ('example.jpg', image_object, 'image/jpeg')
  </code></pre>
  
  <p>...I get the error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "test-gql.py", line 38, in &lt;module&gt;
      'myBlobField': img
    File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 119, in __init__
      self._prepare_parts()
    File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 240, in _prepare_
  parts
      self.parts = [Part.from_field(f, enc) for f in self._iter_fields()]
    File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 488, in from_fiel
  d
      body = coerce_data(field.data, encoding)
    File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 466, in coerce_da
  ta
      return CustomBytesIO(data, encoding)
    File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 529, in __init__
      buffer = encode_with(buffer, encoding)
    File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 410, in encode_wi
  th
      return string.encode(encoding)
  AttributeError: 'JpegImageFile' object has no attribute 'encode'
  </code></pre>
  
  <p>I know from the <a href="https://docs.python.org/2/library/functions.html#open" rel="nofollow noreferrer"><code>open()</code> docs</a> that it returns an object of type <code>file</code>, but the only way I can see in <code>PIL</code> to convert from <code>Image</code> to <code>file</code> is by using <code>save()</code>, which writes it to the disk.  I <em>can</em> write to disk, but I'd rather avoid the step, since I am handling a lot of images.</p>
  
  <p>Is it possible to convert the <code>Image</code> object to <code>file</code> type? Or some other workaround with similar effect?</p>