-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPaint.py
236 lines (185 loc) · 6.67 KB
/
MyPaint.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from tkinter.colorchooser import askcolor
import PIL.Image
from tkinter import filedialog, colorchooser
from tkinter import *
from PIL import ImageTk, Image, ImageDraw
choosenColor = (0,0,0)
drawingImage = None
labelValues = None
global image1
image = None
def main():
global root
root = Tk()
root.title("Basic Colouring & Drawing")
root.geometry("800x600+350+100")
root.configure(bg='#006377')
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=openFile)
filemenu.add_command(label="Draw", command=drawing)
filemenu.add_command(label="Save Image", command=saveImage)
filemenu.add_command(label="Save Drawimg", command=saveDrawing)
filemenu.add_command(label="Clear", command=clear)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
pickColor = Button(root, text="Pick Color", bg="#FFaa00",
fg="black", font="Verdana 12 bold italic", command=getColor).pack()
root.mainloop()
#Dosyadan ilgili resmin çağrılması, uygun boyuta getirilmesi
def openFile():
file_path = filedialog.askopenfilename()
global drawingImage
drawingImage = PIL.Image.open(file_path)
drawingImage = drawingImage.resize((300,300))
global pix
pix = drawingImage.load()
xSize, ySize = drawingImage.size
for i in range(xSize):
for j in range(ySize):
pix[i, j] = vanishNoisesFromPixel(pix[i, j])
addToScreen(drawingImage)
labeling(drawingImage)
#Resmi ekranda göster
def addToScreen( Img ):
render = ImageTk.PhotoImage(Img)
img = Label(root, image=render)
img.imag = render
img.place(x=250, y=100)
img.bind("<Button-1>", printcoords)
#Kullanıcının seçmek istediği renkleri getir
def getColor():
color = askcolor() #renk paleti
r = color [0][0]
g = color [0][1]
b = color[0][2]
global choosenColor
choosenColor = int(r), int(g), int(b)
print(choosenColor)
#Resmi kaydet
def saveImage():
drawingImage.save("C:\\Users\\Toshiba\\Desktop\\ColourTheraPy\\saved.png")
#Resmi başlangıç durumuna getir
def clear():
xSize, ySize = drawingImage.size
for i in range(1, xSize):
for j in range(1, ySize):
if labelValues[i][j]!=1:
pix[i,j] = (255,255,255)
drawingImage.putpixel((i,j),(255,255,255))
render = ImageTk.PhotoImage(drawingImage)
img = Label(root, image=render)
img.image = render
img.place(x=250, y=100)
img.bind("<Button-1>", printcoords)
def labeling (Img):
Img=drawingImage
xSize, ySize = drawingImage.size
for i in range(xSize):
for j in range(ySize):
pix[i, j] = vanishNoisesFromPixel(pix[i, j])
pixelValues = [[0 for x in range(ySize)] for y in range(xSize)]
for i in range(xSize):
for j in range(ySize):
pixelValues[i][j] = converToBinaryValue(pix[i, j])
addToScreen(drawingImage)
for i in range(xSize):
for j in range(ySize):
if i == 0 or j == 0 or i == xSize - 1 or j == ySize - 1:
pixelValues[i][j] = 0
global labelValues
labelValues = [[0 for x in range(ySize)] for y in range(xSize)]
for i in range(xSize):
for j in range(ySize):
labelValues[i][j] = 0
labelCounter = 2
for i in range(1, xSize - 1):
for j in range(1, ySize - 1):
if pixelValues[i][j] == 1:
if pixelValues[i - 1][j] == 1 and pixelValues[i][j - 1] == 1:
if labelValues[i - 1][j] == labelValues[i][j - 1]:
labelValues[i][j] = labelValues[i][j - 1]
else:
labelValues[i][j] = labelValues[i - 1][j]
for t in range(1, xSize):
for k in range(1, ySize):
if labelValues[t][k] == labelValues[i][j - 1]:
labelValues[t][k] = labelValues[i - 1][j]
if t == i and k == j:
break
if t == i and k == j:
break
elif pixelValues[i - 1][j] == 1 or pixelValues[i][j - 1] == 1:
if pixelValues[i - 1][j] == 1:
labelValues[i][j] = labelValues[i - 1][j]
else:
labelValues[i][j] = labelValues[i][j - 1]
else:
labelValues[i][j] = labelCounter
labelCounter += 1
else:
labelValues[i][j] = 1
#Çizim için gerekli fonksiyonlar
def printcoords(event):
print(event.x,event.y)
paintRegion(event.x,event.y)
#Kullanıcıdan alınan koordinasyonları işle
def paintRegion(x,y):
xSize, ySize = drawingImage.size
global labelValues
if labelValues[x][y]!=1:
for t in range(1, xSize):
for k in range(1, ySize):
if labelValues[x][y] == labelValues[t][k]:
global choosenColor
pix[t, k] = choosenColor
drawingImage.putpixel((t, k), choosenColor)
render = ImageTk.PhotoImage(drawingImage)
img = Label(root, image=render)
img.image = render
img.place(x=250, y=100)
img.bind("<Button-1>", printcoords)
def converToBinaryValue(rgbValues):
if len(rgbValues)==4:
r,g,b,f=rgbValues
else:
r,g,b=rgbValues
average=(r+g+b)/3
if average==255 :
return 1
return 0
def vanishNoisesFromPixel( rgbValues ):
if len(rgbValues) == 4:
r, g, b, f = rgbValues
else:
r, g, b = rgbValues
average = (r + g + b) / 3
if average > 200:
return 255, 255, 255
return 0, 0, 0
#Kullanıcı çizim paneli/tuval oluşturma
def drawing():
global image1
width = 400
height = 500
white = (255, 255, 255)
def paint(event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
canvas.create_oval(x1, y1, x2, y2, fill="black", width=5)
draw.line([x1, y1, x2, y2], fill="black", width=5)
root = Tk()
canvas = Canvas(root, width=width, height=height, bg='white')
canvas.pack()
image1 = PIL.Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image1)
canvas.pack(expand=YES, fill=BOTH)
canvas.bind("<B1-Motion>", paint)
#Çizilen resmi kaydet
def saveDrawing():
filename = "C:\\Users\\Toshiba\\Desktop\\ColourTheraPy\\savedrawing.png"
image1.save(filename)
if __name__=='__main__':
main()