-
Notifications
You must be signed in to change notification settings - Fork 1
/
paintApplication.py
executable file
·86 lines (68 loc) · 2.99 KB
/
paintApplication.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
# PAINT APPLICATION WITH ADJUSTABLE COLOR AND BRUSH SIZE
import cv2
import numpy as np
draw = False
window_name = "Paint Brush Application"
color_win_position = [(400, 30), (490,90)]
bgr_track = {'B': 0, 'G': 0, 'R': 0}
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow(window_name)
# Initial color window, showing black
cv2.rectangle(img, color_win_position[0], color_win_position[1], (0,0,0), -1)
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.putText(img, "R: ", (10, 30), font, 0.5, (255,255,255), 1)
img = cv2.putText(img, "G: ", (90, 30), font, 0.5, (255,255,255), 1)
img = cv2.putText(img, "B: ", (170, 30), font, 0.5, (255,255,255), 1)
img = cv2.putText(img, "0", (30, 30), font, 0.5, (255,255,255), 1)
img = cv2.putText(img, "0", (110, 30), font, 0.5, (255,255,255), 1)
img = cv2.putText(img, "0", (190, 30), font, 0.5, (255,255,255), 1)
def nothing(x):
pass
def update_R_value(x):
global font, img, bgr_track
img = cv2.putText(img, f"{bgr_track['R']}", (30, 30), font, 0.5, (0,0,0), 1)
img = cv2.putText(img, f"{x}", (30, 30), font, 0.5, (255,255,255), 1)
bgr_track['R'] = x
def update_G_value(x):
global font, img, bgr_track
img = cv2.putText(img, f"{bgr_track['G']}", (110, 30), font, 0.5, (0,0,0), 1)
img = cv2.putText(img, f"{x}", (110, 30), font, 0.5, (255,255,255), 1)
bgr_track['G'] = x
def update_B_value(x):
global font, img, bgr_track
img = cv2.putText(img, f"{bgr_track['B']}", (190, 30), font, 0.5, (0,0,0), 1)
img = cv2.putText(img, f"{x}", (190, 30), font, 0.5, (255,255,255), 1)
bgr_track['B'] = x
def draw_circle(event, x, y, flags, param):
global draw, img
if event == cv2.EVENT_LBUTTONDOWN:
draw = True
elif event == cv2.EVENT_MOUSEMOVE:
if draw:
cv2.circle(img, (x,y), cv2.getTrackbarPos("Brush Size", window_name),
(cv2.getTrackbarPos("B", window_name),
cv2.getTrackbarPos("G", window_name),
cv2.getTrackbarPos("R", window_name)),
-1)
elif event==cv2.EVENT_LBUTTONUP:
draw = False
cv2.circle(img, (x,y), cv2.getTrackbarPos("Brush Size", window_name),
(cv2.getTrackbarPos("B", window_name),
cv2.getTrackbarPos("G", window_name),
cv2.getTrackbarPos("R", window_name)),
-1)
cv2.createTrackbar("R", window_name, 0 ,255, update_R_value)
cv2.createTrackbar("G", window_name, 0, 255, update_G_value)
cv2.createTrackbar("B", window_name, 0, 255, update_B_value)
cv2.createTrackbar("Brush Size", window_name, 1, 8, nothing)
cv2.setMouseCallback(window_name, draw_circle)
while(1):
cv2.imshow(window_name, img)
key = cv2.waitKey(1) & 0xff
if key==ord('q'):
break
b = cv2.getTrackbarPos("B", window_name)
g = cv2.getTrackbarPos("G", window_name)
r = cv2.getTrackbarPos("R", window_name)
cv2.rectangle(img, color_win_position[0], color_win_position[1], (b,g,r), -1)
cv2.destroyAllWindows()