def decode_img()

in sam-code/img_api/app.py [0:0]


def decode_img(img_data,mime_type,xdim,ydim):
    # If the content type claims we received an image/binary:
    if re.match("image/",mime_type) or mime_type=='application/x-www-form-urlencoded':
        # This function relies entirely on PIL to identify image type, no mime type checking
        img_buffer = BytesIO(img_data)
        img_buffer.seek(0)
        image = Image.open(img_buffer)
    else:
        # Render the plain text as black text on a white background
        image = Image.new("RGB", (xdim,ydim), (255, 255, 255))
        draw  = ImageDraw.Draw(image)
        draw.multiline_text((0,0),img_data.decode('utf-8'),fill=(0,0,0))
    if image.mode == 'P':
        image = image.convert('RGB')
    return image