in sam-code/img_api/app.py [0:0]
def encode_img(image,accept_string):
# Convert image data into an image file and base64 encode it
mime_type = get_mime_type(accept_string)
if mime_type in known_conversions:
# Use BytesIO to write to a virtual file
img_buffer = BytesIO()
image.save(img_buffer,format=known_conversions[mime_type])
img_binary = img_buffer.getvalue()
img_buffer.close()
return { "success": True, "mimetype": mime_type, "data": img_binary }
else:
return { "success": False, "mimetype": "text/plain", "data": "Unknown encoding requested" }