code: def pack_image(filename, max_size, form_field='image'):
          
          try:
              if os.path.getsize(filename) > (max_size * 1024):
                  raise IdeaScalyError('File is too big, must be less than %skb.' % max_size)
          except os.error as e:
              raise IdeaScalyError('Unable to access file: %s' % e.strerror)
  
          # build the mulitpart-formdata body
          fp = open(filename, 'rb')
  
          # image must be gif, jpeg, or png
          file_type = mimetypes.guess_type(filename)
          if file_type is None:
              raise IdeaScalyError('Could not determine file type')
          file_type = file_type[0]
          if file_type not in ['image/gif', 'image/jpeg', 'image/png']:
              raise IdeaScalyError('Invalid file type for image: %s' % file_type)
  
          if isinstance(filename, six.text_type):
              filename = filename.encode('utf-8')
  
          BOUNDARY = b'Id34Sc4ly'
          body = list()
          body.append(b'--' + BOUNDARY)
          body.append('content-disposition: form-data; name="{0}";'
                      ' filename="{1}"'.format(form_field, filename)
                      .encode('utf-8'))
          body.append('content-type: {0}'.format(file_type).encode('utf-8'))
          body.append(b'')
          body.append(fp.read())
          body.append(b'--' + BOUNDARY + b'--')
          body.append(b'')
          fp.close()
          body = b'\r\n'.join(body)
          body_length = str(len(body))
  
          # build headers
          headers = {
              'content-type': 'multipart/form-data; boundary={0}'.format(BOUNDARY),
              'content-length': body_length
          }
  
          return headers, body
docs: Pack an image from file into multipart-formdata post body