-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.py
65 lines (53 loc) · 2.01 KB
/
drawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from PIL import ImageTk, ImageDraw, ImageFilter
import PIL
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
def imageprepare(argv):
im = PIL.Image.open(argv).convert('L')
width = float(im.size[0])
height = float(im.size[1])
newImage = PIL.Image.new('L', (28, 28), (255))
img = im.resize((28, 28), PIL.Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
tv = list(img.getdata())
tva = [(255 - x) * 1.0 / 255.0 for x in tv]
return tva
class Draw_field:
def __init__(self, width = 560, height = 560):
self.width = 560
self.height = 560
center = height//2
self.white = (255, 255, 255)
green = (0,128,0)
root = Tk()
self.cv = Canvas(root, width=self.width, height=self.height, bg='white')
self.cv.pack()
self.image1 = PIL.Image.new("RGB", (self.width, self.height), self.white)
self.draw = ImageDraw.Draw(self.image1)
self.cv.pack(expand=YES, fill=BOTH)
self.cv.bind("<B1-Motion>", self.paint)
button=Button(text="predict",command=self.predict)
button2 = Button(text='reset' , command = self.reset)
button.pack()
button2.pack()
root.mainloop()
def predict(self):
filename = "temp.png"
self.image1.save(filename)
image = imageprepare(filename)
image = np.array(image, dtype='float').reshape(784,1)
pixels = image.reshape((28, 28))
plt.imshow(pixels, cmap= 'gray_r')
plt.show()
#Do stuff ..
def paint(self, event):
x1, y1 = (event.x - 10), (event.y - 10)
x2, y2 = (event.x + 20), (event.y + 10)
self.cv.create_oval(x1, y1, x2, y2, fill="black",width=20)
self.draw.line([x1, y1, x2, y2],fill="black",width=30)
def reset(self):
self.cv.delete("all")
self.image1 = PIL.Image.new("RGB", (self.width, self.height), self.white)
self.image1.save("temp.png")
self.draw = ImageDraw.Draw(self.image1)
draw = Draw_field()