import base64


def img_file_to_b64str(filename, urlsafe=False):
    """
    Open an image file, and convert it into Base64 string.
    This can help you store your image in a simple text blob of your SQL databases.
    :param filename: The file you want to convert
    :param urlsafe: Trigger using URL-Safe format. You'd better set it true if you want to post these string to an HTML page.
    :return: Converted string.
    """
    # Open image file as byte
    file_byte = open(filename, 'rb').read()

    if urlsafe:
        file_base64 = base64.urlsafe_b64encode(file_byte)
    else:
        file_base64 = base64.standard_b64encode(file_byte)

    file_base64_string = file_base64.decode('utf-8')

    return file_base64_string