当前位置: 首页 > 工具软件 > Image2Base64 > 使用案例 >

python3-图片转BASE64格式

靳金鹏
2023-12-01

python3图片转BASE64格式

话不多说,直接上代码,有空再补充啥是BASE64(一般不会了)

import base64


def img2base64(image_bytes):
    with open(image_bytes, "rb") as fb:
        img = fb.read()
        base64_bytes = base64.b64encode(img)
        base64_str = base64_bytes.decode("utf-8")

    return base64_str

if __name__ == "__main__":
    img_path = r"path_to_your_img_dir.jpg"
    base64_img = img2base64(img_path)
 类似资料: