0

I am using Latex and matplotlib library to get the output in canvas image
But i need the output in the form of string.

here is my tkinter code

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, fill=BOTH, expand=1)



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

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


here is my output enter image description here

Above output is in canvas image i want to convert it into string format
is there any way to convert canvas image into string format
please help me out with this issue

satyam jha
  • 23
  • 6
  • 1
    What exactly do you mean? Do you need the `"\int_{a}^b f(x)dx"` back from the canvas? – TheLizzard Mar 27 '21 at 22:14
  • No i want that canvas output image in the string form which i can able to copy or paste. – satyam jha Mar 28 '21 at 03:32
  • [Encoding an image file with base64](https://stackoverflow.com/questions/3715493/encoding-an-image-file-with-base64)? – JacksonPro Mar 28 '21 at 04:06
  • yes i use base64 `imagedata = base64.b64encode(canvas)` but this gives me error `TypeError: a bytes-like object is required, not 'FigureCanvasTkAgg'` – satyam jha Mar 28 '21 at 04:31
  • You have to get the data on the canvas as an image. Look at [this](https://stackoverflow.com/a/10432892/11106801). Then you can open the file (in binary mode - `"rb"`) and read the data. You can then encode that data – TheLizzard Mar 28 '21 at 09:20

0 Answers0