title: How to return binary data from lambda function in AWS in Python?
body: <p>I cannot get python lambda to return binary data. The <a href="https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html" rel="noreferrer">node-template for thumbnail images</a> works fine but I cannot get a python lambda to work. Below is the relevant lines from my lambda. The <code>print("image_data " + image_64_encode)</code> line prints a base64 encoded image to the logs.</p>
  
  <pre><code>def lambda_handler(event, context):
      img_base64 = event.get('base64Image')
      if img_base64 is None:
          return respond(True, "No base64Image key")
  
      img = base64.decodestring(img_base64)
      name = uuid.uuid4()
      path = '/tmp/{}.png'.format(name)
  
      print("path " + path)
  
      image_result = open(path, 'wb')
      image_result.write(img)
      image_result.close()
  
      process_image(path)
  
      image_processed_path = '/tmp/{}-processed.png'.format(name)
      print("image_processed_path " + image_processed_path)
      image_processed = open(image_processed_path, 'rb')
      image_processed_data = image_processed.read()
      image_processed.close()
      image_64_encode = base64.encodestring(image_processed_data)
  
      print("image_data " + image_64_encode)
  
  
      return respond(False, image_64_encode)
  
  
  def respond(err, res):
      return {
          'statusCode': '400' if err else '200',
          'body': res,
          'headers': {
              'Content-Type': 'image/png',
          },
          'isBase64Encoded': 'true'
      }
  </code></pre>
  
  <p>Any pointers to what I'm doing wrong?</p>