找回密码
 立即注册
搜索
热搜: 微信小游戏
查看: 211|回复: 0

用python将matplotlib画的图直接转为base64

[复制链接]

84

主题

75

回帖

655

积分

管理员

积分
655
管理员 发表于 2024-7-11 16:26:25 | 显示全部楼层 |阅读模式
import matplotlib.pyplot as plt
import base64
from io import BytesIO

# 生成示例图形
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')

# 创建一个内存中的文件对象
buffer = BytesIO()

# 保存图形到内存中的文件对象
plt.savefig(buffer, format='png')
buffer.seek(0)

# 对内存中的文件对象进行Base64编码
encoded_image = base64.b64encode(buffer.read())

# 将编码后的图像转换为包含图片类型信息的Base64字符串
encoded_string = 'data:image/png;base64,' + encoded_image.decode('utf-8')

plt.show()  # 显示图形

print(encoded_string)  # 打印包含图片类型信息的Base64编码字符串
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|小黑屋|微信搜索指数|独开 ( 粤ICP备2023140300号-3 )

GMT+8, 2024-9-8 09:38 , Processed in 0.024202 second(s), 19 queries .

快速回复 返回顶部 返回列表