Skip to content

Instantly share code, notes, and snippets.

@abhamra
Created December 15, 2020 05:29
Show Gist options
  • Save abhamra/8ec558c31d913312b26f4f9f31f4df3a to your computer and use it in GitHub Desktop.
Save abhamra/8ec558c31d913312b26f4f9f31f4df3a to your computer and use it in GitHub Desktop.
#test.py (drawing)
from PIL import ImageTk, Image, ImageDraw
import PIL
from tkinter import *
#simple.py (parsing)
import mathpix
import json
#sympytest.py (plot)
import sympy as sp
from sympy.plotting import plot
from sympy.parsing.sympy_parser import *
from sympy.parsing.latex import *
#DRAWING SECTION
width = 1000
height = 400
center = height//2
white = (255, 255, 255)
green = (0,128,0)
def paint(event):
# python_green = "#476042"
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
cv.create_oval(x1, y1, x2, y2, fill="black",width=5)
draw.line([x1, y1, x2, y2],fill="black",width=5)
root = Tk()
# Tkinter create a canvas to draw on
cv = Canvas(root, width=width, height=height, bg='white')
cv.pack()
def save():
filename = "../images/equation.jpg"
image1.save(filename)
#def clear():
#cv.delete("all")
# PIL create an empty image and draw object to draw on
# memory only, not visible
image1 = PIL.Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image1)
# do the Tkinter canvas drawings (visible)
# cv.create_line([0, center, width, center], fill='green')
cv.pack(expand=YES, fill=BOTH)
cv.bind("<B1-Motion>", paint)
# do the PIL image/draw (in memory) drawings
# draw.line([0, center, width, center], green)
# PIL image can be saved as .png .jpg .gif or .bmp file (among others)
# filename = "my_drawing.png"
# image1.save(filename)
button=Button(text="save",command=save)
#button2 = Button(text="clear", command=clear)
#button2.pack()
button.pack()
root.mainloop()
#PARSING SECTION
r = mathpix.latex({
'src': mathpix.image_uri('../images/equation.jpg'),
'formats': ['latex_simplified']
})
#print(json.dumps(r, indent=4, sort_keys=True))
print(r['latex_simplified'])
latexToGraph = r['latex_simplified']
#GRAPHING SECTION
x, y, z, t = sp.symbols('x y z t')
k, m, n = sp.symbols('k m n', integer=True)
# f, g, h = sp.symbols('f g h', cls=Function)
#expr1 = parse_latex(r"x^{2}")
#sympy plotting stuff
p1 = plot(parse_latex(latexToGraph), show=False, title=latexToGraph, xlim=[-10, 10], ylim=[-10, 10])
#p2 = plot(expr2, 5, show=False)
#p1.extend(p2)
p1.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment