from io import StringIO


def render_pil(image, fmt="jpg"):
    """
    Render PIL Image to base64.
    :param image:   given image.
    :param fmt:     image format.
    :return:        rendered image.
    """
    if not callable(getattr(image, "save", None)):
        return None
    output = StringIO()
    image.save(output, format=fmt)
    contents = output.getvalue()
    output.close()
    return contents, "image/%s" % fmt