微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python tkinter中将画布图像转换为字符串

如何解决如何在python tkinter中将画布图像转换为字符串

我正在使用 Latex 和 matplotlib 库来获取画布图像中的输出
但我需要字符串形式的输出

这是我的 tkinter 代码

import matplotlib
import matplotlib.pyplot as plt
# import numpy as np
# import base64

from matplotlib.backends.backend_tkagg import figureCanvasTkAgg
matplotlib.use('TkAgg')

from tkinter import *

def graph(text):
    tmptext = entry.get()
    tmptext = "$"+tmptext+"$"

    ax.clear()
    ax.text(0,0.9,tmptext,fontsize = 16)
    canvas.draw()
    # s,(width,height) = canvas.print_to_buffer()
    # x = np.fromstring(s,np.uint8).reshape((height,width,4))
    # y = np.array_str(x)
    # print(y)
    # imagedata = base64.b64encode()
    # print(imagedata)
    # canvast = Canvas(root,width=400,height=500)
    # canvast.pack()
    # canvast.create_text(100,10,text=""")
     
    


root = Tk()

mainframe = Frame(root)
mainframe.pack()

text = StringVar()
entry = Entry(mainframe,width=70,textvariable=text)
entry.pack()

label = Label(mainframe)
label.pack()

fig = matplotlib.figure.figure(figsize=(10,4),dpi=100)
ax = fig.add_subplot(111)


canvas = figureCanvasTkAgg(fig,master=label)
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
canvas._tkcanvas.pack(side=TOP,expand=1)



ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

root.bind('<Return>',graph)
root.mainloop()


这是我的输出

enter image description here

以上输出是画布图像,我想将其转换为字符串格式
有什么方法可以将画布图像转换为字符串格式
请帮我解决这个问题

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。