【Python】将图片转为base64数据流

图片转base64

    def IconToBase64(self, UIPhoto):
        with open(UIPhoto, 'rb') as image_file:
            base64_string = base64.b64encode(image_file.read()).decode('utf-8')
            # 如果要得到Data URL,可以加上前缀
            data_url = f"data:image/jpeg;base64,{base64_string}"
            r = data_url.split(",")
            b = r[1]
            print(r)
            print(b)
            return b

运行

if __name__ == '__main__':

    a = r"D:\icon\列A\1.jpeg"

    demo = IconColor().IconToBase64(a)

执行结果

打印r
image
打印b
image

posted @ 2026-02-09 10:58  Phoenixy  阅读(7)  评论(0)    收藏  举报