import os
import mimetypes
import base64




def encode_image(path: str):
    # 根据文件扩展名获取 MIME 类型
    mime_type, _ = mimetypes.guess_type(path)
    if mime_type is None:
        mime_type = 'image/jpeg'  # 默认使用 jpeg 类型
    # 将图片编码为 base64 字符串
    with open(path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
        base64Str = encoded_string.decode("utf-8")
        return f"data:{mime_type};base64,{base64Str}"
