-
Notifications
You must be signed in to change notification settings - Fork 0
/
GruvPaint.py
542 lines (478 loc) · 21.7 KB
/
GruvPaint.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
from tkinter import *
from tkinter import filedialog
from PIL import ImageGrab
from PIL import Image,ImageTk
import time
import math
class Paint(object):
def __init__(self):
self.root = Tk()
self.root.configure(background = '#282828')
#Colours
self.colourPicker = LabelFrame(self.root, relief = FLAT, bg = '#282828', highlightthickness = 0)
self.colourPicker.grid(row = 0, column = 0, columnspan=2, rowspan = 5)
colours = ['#282828', '#cc241d', '#98971a', '#d79921', '#458588',
'#b16286', '#689d6a', '#a89984', '#d65d0e', '#ebdbb2']
a = b = 0
for bruh in colours:
Button(self.colourPicker, bg = bruh, width = 3, relief = FLAT, height = 1, highlightthickness = 0,
command = lambda col = bruh : self.updateColour(col)).grid(row = a, column = b)
a += 1
if a == 5:
a = 0
b = 1
#Pen
self.penImg = PhotoImage(file = "Assets/paintbrushGruv.png")
self.penButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command=self.usePen, image = self.penImg)
self.penButton.place(x = 10, y = 260)
#Eraser
self.eraserImg = PhotoImage(file = "Assets/eraserGruv.png")
self.eraserButton = Button(self.root, highlightthickness = 0,relief = FLAT, bg = '#282828',
command=self.useEraser, image = self.eraserImg)
self.eraserButton.place(x = 75, y = 260)
#ColourDropper
self.pickerImg = PhotoImage(file = "Assets/pickerGruv.png")
self.pickerButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command = lambda: self.setActiveTool("dropper"), image = self.pickerImg)
self.pickerButton.place(x = 5, y = 340)
#Shapes Dropdown Menu
self.shapesImg = PhotoImage(file = "Assets/shapesGruv.png")
self.shapesButton = Menubutton(self.root, highlightthickness=0, fg= '#ebdbb2',
relief=FLAT, bg='#282828', image = self.shapesImg)
self.shapesButton.place(x = 10, y = 450)
self.shapesButton.menu = Menu(self.shapesButton, tearoff=0)
self.shapesButton['menu'] = self.shapesButton.menu
# Add shapes to the shapes dropdown menu
self.addOption(self.shapesButton.menu, "Rectangle")
self.addOption(self.shapesButton.menu, "Curve")
self.addOption(self.shapesButton.menu, "Circle")
self.addOption(self.shapesButton.menu, "Line")
self.addOption(self.shapesButton.menu, "Oval")
self.addOption(self.shapesButton.menu, "Star")
#Zoom
self.zoomImg = PhotoImage(file = "Assets/magnifyGruv.png")
self.zoomButton = Button(self.root, highlightthickness = 0,relief = FLAT, bg = '#282828',
command = lambda: self.setActiveTool("zoom"), image = self.zoomImg)
self.zoomButton.place(x = 75, y = 450)
#Bucket
self.fillImg = PhotoImage(file = "Assets/bucketGruv.png")
self.bucketButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command = lambda: self.setActiveTool("bucket"), image = self.fillImg)
self.bucketButton.place(x = 10, y = 530)
#pixelJump
self.pJump = Entry(self.root, width = 3, highlightthickness=0, justify="center",
bg= '#ebdbb2', relief = FLAT, fg = '#282828')
self.pJump.place(x = 80, y = 540)
self.pJump.insert(0, "20")
# N-Polygon Button
self.N = PhotoImage(file = "Assets/hexagonGruv.png")
self.nPolygonButton = Button(self.root, highlightthickness=0, relief = FLAT, bg = '#282828',
command = lambda: self.setActiveTool("ngon"), image = self.N)
self.nPolygonButton.place(x = 10, y = 610)
# N Entry
self.nEntry = Entry(self.root, width = 3, highlightthickness=0, justify="center",
bg= '#ebdbb2', relief = FLAT, fg = '#282828')
self.nEntry.place(x = 80, y = 620)
self.nEntry.insert(0, "3")
#Select
self.select = PhotoImage(file = "Assets/selectGruv.png")
self.selectButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command = lambda: self.setActiveTool("sele"), image = self.select)
self.selectButton.place(x = 10, y = 720)
#Clear
self.trashImg = PhotoImage(file = "Assets/binGruv.png")
self.clearButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command = self.clearScreen, image = self.trashImg)
self.clearButton.place(x = 75, y = 720)
#Save
self.save = PhotoImage(file = "Assets/downloadGruv.png")
self.saveButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command = self.saveCanvas, image = self.save)
self.saveButton.place(x = 5, y = 800)
#Load
self.load = PhotoImage(file = "Assets/uploadGruv.png")
self.loadButton = Button(self.root, highlightthickness = 0, relief = FLAT, bg = '#282828',
command = self.loadCanvas, image = self.load)
self.loadButton.place(x = 75, y = 800)
#Pen Width
self.sizeSlider = Scale(self.root, from_=40, to=20, highlightthickness = 0,
fg = '#ebdbb2', orient=VERTICAL, bg = '#282828')
self.sizeSlider.grid(row=16, column=0, rowspan = 4, sticky='NS', padx=25, pady = 20)
self.can = Canvas(self.root, bg='#ebdbb2', width=1920, height=1080)
self.can.grid(rowspan = 20, column=2, row = 0)
self.setup()
self.root.mainloop()
def setup(self):
self.defaultColour = '#282828'
self.backgroundColour = '#ebdbb2'
#pen
self.prev_x = None
self.prev_y = None
self.eraserActive = False
#shapes
self.firstX = None
self.firstY = None
self.shapeItem = None
#selection
self.finalX = None
self.finalY = None
#loadcanvas + select
self.image = None
#zoom
self.zoomScale = 2
self.zoomed = False
self.zoomX = None
self.zoomY = None
#general
self.lineWidth = 5
self.colour = self.defaultColour
self.activeTool = "pen"
self.shapes = ("line", "curv", "rect", "oval", "squa", "circ", "star", "ngon")
self.controlPoints = []
#binds
self.can.bind('<Button-1>', self.tap)
self.can.bind('<B1-Motion>', self.drag)
self.can.bind('<ButtonRelease-1>', self.lift)
def addOption(self, menu, label):
menu.add_command(label=label, command=lambda: self.setActiveTool(label))
def usePen(self):
self.can.delete(self.shapeItem)
self.setActiveTool("pen")
self.eraserActive = False
def getHexOfPixel(self, x, y):
#taking a 'screenshot'
image = ImageGrab.grab()
#getting the colours from that screenshot
rgb = image.getpixel((x, y))
#converts rgb code to hex code and returns
return '#%02x%02x%02x' % rgb
def getPixelColour(self, event):
#Coordinates of clicked pixel
# self.root.winfo_x = starting x pixel of program on the screen
# 140 = compensate for the button menu
# event.x = x pixel of JUST the click point on the canvas from the NW anchor
x = self.root.winfo_x() + 140 + event.x
y = self.root.winfo_y() + event.y
#get hex value
hex = self.getHexOfPixel(x, y)
#calls updateColour function with the hex
self.updateColour(hex)
#changing the tool back to pen again
self.setActiveTool("pen")
self.eraserActive = False
def updateColour(self, col):
print("COLOUR UPDATED:",col)
self.eraserActive = False
self.colour = col
def clearScreen(self):
print("Screen cleared.")
if self.activeTool != "move": #usual
self.can.delete("all")
else: #clear when area is selected
self.can.create_rectangle(self.firstX, self.firstY, self.finalX, self.finalY,
width = 0, fill = self.backgroundColour)
self.setActiveTool("select")
self.finalX = None
self.finalY = None
self.firstX = None
self.firstY = None
self.can.delete(self.shapeItem)
def useEraser(self):
self.setActiveTool("pen")
self.eraserActive = True
def saveCanvas(self):
print("Image saved.")
#filename = filedialog.askopenfilename(initialdir = "/home/vagabond/Documents/Paint/Saved",title = "Select file",filetypes = (("png files","*.png"), ("all files", "*.*")))
saveDate = time.strftime("%I:%M:%S", time.localtime())
dir = "/home/vagabond/Documents/Paint/Saved/canvas_" + saveDate + ".png"
x = self.root.winfo_x() + 140
y = self.root.winfo_y()
ImageGrab.grab(bbox = (x, y, x + 1780, y + 1080)).save(dir)
def loadCanvas(self):
filename = filedialog.askopenfilename(initialdir = "/home/vagabond/Documents/Paint/Saved",title = "Select file",filetypes = (("png files","*.png"), ("all files", "*.*")))
self.image = ImageTk.PhotoImage(Image.open(filename))
self.can.create_image(0, 0, image = self.image, anchor = NW)
self.can.delete(self.image)
print("Image loaded.")
def paint(self, event):
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.eraserActive:
paintColour = self.backgroundColour
else:
paintColour = self.colour
if self.prev_x and self.prev_y:
self.can.create_line(self.prev_x, self.prev_y, event.x, event.y,
width=self.lineWidth, fill=paintColour,
capstyle=ROUND, smooth=TRUE)
self.prev_x = event.x
self.prev_y = event.y
def fillColour(self, event):
pixelJump = 20
pJump = self.pJump.get()
if pJump.isdigit():
if int(pJump) >= 5 and int(pJump) <= 20:
pixelJump = int(pJump)
else:
print("Invalid value of pixelJump.")
#Coordinates of clicked pixel
x = self.root.winfo_x() + 140 + event.x
y = self.root.winfo_y() + event.y
#For drawing later
xMinus = (140 + self.root.winfo_x())
yMinus = self.root.winfo_y()
#For upper canvas bounds
xLimit = self.root.winfo_x() + 1920
yLimit = self.root.winfo_y() + 1080
#hexcode of click point
originalHex = self.getHexOfPixel(x, y)
#Hardcoding in the first point
oldX, oldY = x, y
pixels = [(x, y)]
pixelCount = 1;
#Big brain memoization idea holy fucking shit
pixelsChecked = set()
#While pixels exists
while pixels:
#Assign top-most value in list to newX & newY
newX, newY = pixels.pop(0)
pixelCount -= 1
print("Current number of pixels to check:",pixelCount)
#if within bounds and un-visited
if 0 < newX < xLimit and 0 < newY < yLimit and (newX, newY) not in pixelsChecked:
#Get the pixel color at the current position
newHex = self.getHexOfPixel(newX, newY)
#Check if the pixel color is the same as the original color
if newHex == originalHex:
#Set the fill color at the current position
self.can.create_line(oldX - xMinus, oldY - yMinus, newX - xMinus, newY - yMinus,
width = pixelJump, smooth=True, capstyle=ROUND, fill = self.colour)
#Adding cardinal directions to pixels
pixels.extend([(newX + pixelJump, newY), (newX - pixelJump, newY),
(newX, newY + pixelJump), (newX, newY - pixelJump)])
pixelCount += 4
pixelsChecked.add((newX, newY))
#Replacing values
oldX, oldY = newX, newY
def setActiveTool(self, tool):
tool = tool.lower()
if len(tool) > 4:
tool = tool[0:4]
self.activeTool = tool
print("NEW TOOL: " + tool)
def updateFirstPixel(self, event):
self.firstX = event.x
self.firstY = event.y
def zoomer(self, event):
if not self.zoomed:
print("ZOOMED IN.")
x, y = event.x, event.y
self.zoomX, self.zoomY = x, y
self.can.scale('all', x, y, self.zoomScale, self.zoomScale)
else:
print("ZOOMED OUT.")
self.can.scale('all', self.zoomX, self.zoomY, 1/self.zoomScale, 1/self.zoomScale)
self.zoomed = not self.zoomed
self.setActiveTool("pen")
def drawLine(self, event):
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.shapeItem:
self.can.delete(self.shapeItem)
self.shapeItem = self.can.create_line(self.firstX, self.firstY, event.x, event.y,
width=self.lineWidth, fill = self.colour,
capstyle=ROUND, smooth=TRUE)
def drawCurve(self, event):
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.shapeItem:
self.can.delete(self.shapeItem)
if event.x >= self.firstX and event.y <= self.firstY:
startAngle = 0
endAngle = 180
elif event.x <= self.firstX and event.y <= self.firstY:
startAngle = 180
endAngle = 0
elif event.x >= self.firstX and event.y >= self.firstY:
startAngle = 0
endAngle = -180
elif event.x <= self.firstX and event.y >= self.firstY:
startAngle = -180
endAngle = 0
self.shapeItem = self.can.create_arc(self.firstX, self.firstY, event.x, event.y,
start = startAngle, extent = endAngle - startAngle,
width=self.lineWidth, fill=self.colour, style = ARC)
def drawRect(self, event, squareBool = False):
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.shapeItem:
self.can.delete(self.shapeItem)
if not squareBool:
self.shapeItem = self.can.create_rectangle(self.firstX, self.firstY, event.x, event.y,
width=self.lineWidth, outline = self.colour)
else:
length = max(abs(event.x - self.firstX), abs(event.y - self.firstY))
self.shapeItem = self.can.create_rectangle(self.firstX, self.firstY,
self.firstX + length, self.firstY + length,
width=self.lineWidth, outline = self.colour)
def drawOval(self, event, circleBool = False):
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.shapeItem:
self.can.delete(self.shapeItem)
if not circleBool:
self.shapeItem = self.can.create_oval(self.firstX, self.firstY, event.x, event.y,
width=self.lineWidth, outline = self.colour)
else:
length = max(abs(event.x - self.firstX), abs(event.y - self.firstY))
self.shapeItem = self.can.create_oval(self.firstX, self.firstY,
self.firstX + length, self.firstY + length,
width=self.lineWidth, outline = self.colour)
def drawStar(self, event):
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.shapeItem:
self.can.delete(self.shapeItem)
xMid = (event.x + self.firstX) / 2
yMid = (event.y + self.firstY) / 2
d = math.dist([self.firstX, self.firstY], [event.x, event.y])
angle = math.atan2(event.y - self.firstY, event.x - self.firstX)
points = []
for i in range(10):
if i % 2 == 0:
theta = angle + i * math.pi / 5
x = xMid + d * math.cos(theta)
y = yMid + d * math.sin(theta)
else:
theta = angle + i * math.pi / 5
x = xMid + (d / 2) * math.cos(theta)
y = yMid + (d / 2) * math.sin(theta)
points.append(x)
points.append(y)
self.shapeItem = self.can.create_polygon(points, fill='', outline=self.colour, width=self.lineWidth)
def drawPoly(self, event):
N = 3
n = self.nEntry.get() # Get the value of N from the entry
if n.isdigit():
if int(n) >= 3:
N=int(n)
elif int(n) <= 3:
print("Invalid input for N.")
self.lineWidth = self.sizeSlider.get()
if self.zoomed:
self.lineWidth *= 2
if self.shapeItem:
self.can.delete(self.shapeItem)
points = []
xMid = event.x
yMid = event.y
radius = abs(self.firstX - event.x)
angle = -math.pi / 2 # Starting angle (-90 degrees) for proper alignment
for i in range(N):
x = xMid + radius * math.cos(2 * math.pi * i / N + angle)
y = yMid + radius * math.sin(2 * math.pi * i / N + angle)
points.append((x, y))
self.shapeItem = self.can.create_polygon(points, fill='', outline=self.colour, width=self.lineWidth)
def makeSelection(self, event):
if self.shapeItem:
self.can.delete(self.shapeItem)
self.shapeItem = self.can.create_rectangle(self.firstX, self.firstY, event.x, event.y,
width=1, outline = self.defaultColour)
def moveTap(self, event):
x = self.root.winfo_x() + 140
y = self.root.winfo_y()
self.can.delete(self.shapeItem)
self.can.delete(self.image)
a, b, c, d = self.firstX, self.firstY, self.finalX, self.finalY
if self.finalX < self.firstX:
self.firstX, self.finalX = self.finalX, self.firstX
if self.finalY < self.firstY:
self.firstY, self.finalY = self.finalY, self.firstY
#Image object formed.
saveDate = time.strftime("%I:%M:%S", time.localtime())
dir = "/home/vagabond/Documents/Paint/MovingJunk/cut_" + saveDate + ".png"
#saving as image
ImageGrab.grab(bbox = (x + self.firstX + 1, y + self.firstY + 1, x + self.finalX, y + self.finalY)).save(dir)
#deleting the backside
self.can.create_rectangle(a, b, c, d, width = 0, fill = self.backgroundColour)
#importing as image
image = ImageTk.PhotoImage(Image.open(dir))
self.image = self.can.create_image(a, b, anchor = NW, image = image)
self.image.show()
def moveImage(self, event):
if self.prev_x and self.prev_y:
x = event.x - self.prev_x
y = event.y - self.prev_y
self.can.move(self.image, x, y)
self.prev_x, self.prev_y = event.x, event.y
def drag(self, event):
match self.activeTool:
case "pen":
self.paint(event)
case "line":
self.drawLine(event)
case "squa":
self.drawRect(event, True)
case "circ":
self.drawOval(event, True)
case "ngon":
self.drawPoly(event)
case "star":
self.drawStar(event)
case "oval":
self.drawOval(event)
case "rect":
self.drawRect(event)
case "curv":
self.drawCurve(event)
case "sele":
self.makeSelection(event)
case "move":
self.moveImage(event)
def tap(self, event):
if self.activeTool == "drop":
self.getPixelColour(event)
elif self.activeTool == "buck":
self.fillColour(event)
elif (self.activeTool in self.shapes) or (self.activeTool == "sele"):
self.updateFirstPixel(event)
elif self.activeTool == "move":
self.moveTap(event)
elif self.activeTool == "zoom":
self.zoomer(event)
def lift(self, event):
if self.activeTool == "pen":
self.resetXY()
elif self.activeTool in self.shapes:
self.resetShape()
elif self.activeTool == "sele":
self.finishSelection(event)
elif self.activeTool == "move":
self.finishMoving()
def finishMoving(self):
self.firstX = None
self.firstY = None
self.prev_x = None
self.prev_y = None
self.image = None
self.setActiveTool("pen")
def resetShape(self):
print("Shape drawn.")
self.shapeItem = None
self.firstX = None
self.firstY = None
def finishSelection(self, event):
if self.shapeItem:
self.finalX, self.finalY = event.x, event.y
self.setActiveTool("move")
def resetXY(self):
self.prev_x, self.prev_y = None, None
if __name__ == '__main__':
Paint()