-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_grabber.py
130 lines (103 loc) · 5.41 KB
/
color_grabber.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
# MIT License
#
# Copyright (c) 2018 Shane Bielefeld
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from pynput.mouse import Controller # for getting cursor position
from mss import mss # for screenshots
import time
import tkinter as tk # for gui
from PIL import Image, ImageTk, ImageDraw # for manipulating and displaying images in gui
from color_converter import convert_rgb_to_hsv
## called to update all the moving parts
def update(root, sct, mouse, image_label, color_swatch_frame, color_value_labels):
## get cursor position
x, y = mouse.position
#print('x: ' + str(x) + ', y: ' + str(y))
## snapshot of the area around cursor
frame = sct.grab({ 'top': y - 5, 'left': x - 5, 'width': 11, 'height': 11 })
img = Image.frombytes('RGB', frame.size, frame.rgb)
img = img.resize((110, 110))
draw = ImageDraw.Draw(img)
draw.rectangle(((50, 50), (60, 60)), outline=200)
img = ImageTk.PhotoImage(image=img)
image_label.configure(image=img)
image_label._image_cache = img # avoid garbage collection
root.update()
## update color value labels
rgb = frame.pixels[int(frame.width/2)][int(frame.height/2)]
color_value_labels[0].configure(text='B: ' + str(rgb[2]))
color_value_labels[1].configure(text='G: ' + str(rgb[1]))
color_value_labels[2].configure(text='R: ' + str(rgb[0]))
h, s, v = convert_rgb_to_hsv(rgb)
color_value_labels[3].configure(text='H: ' + str(h))
color_value_labels[4].configure(text='S: ' + str(s))
color_value_labels[5].configure(text='V: ' + str(v))
## update color swatch
color_swatch_frame.configure(bg='#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2]))
root.after(20, func=lambda: update(root, sct, mouse, image_label, color_swatch_frame, color_value_labels))
## GUI Colors
WINDOW_BG_COLOR = '#%02x%02x%02x' % (236, 236, 236)
if __name__ == '__main__':
root = tk.Tk()
root.title('Better Digital Color Meter')
root.configure(bg=WINDOW_BG_COLOR)
## create a PanedWindow in which to shape top level stuff
pane_window = tk.PanedWindow(root, bg=WINDOW_BG_COLOR)
pane_window.pack(fill=tk.BOTH, expand=1)
## left pane - image under cursor
left_pane = tk.Frame(pane_window, width=110, height=110)#, height=110, width=140)
left_pane.pack_propagate(0) # don't shrink
pane_window.add(left_pane, padx=15, pady=15)
## create frame for image - this ensures size stays right
image_frame = tk.Frame(left_pane, height=110, width=110, highlightthickness=0.5, highlightbackground='#%02x%02x%02x' % (218, 218, 218), bd=0)
image_frame.pack_propagate(0) # don't shrink
image_frame.pack()
## create a label for the image to sit in, but leave it empty for now
image_label = tk.Label(image_frame)#, highlightthickness=1, highlightbackground='yellow', bd=0)
image_label.pack(fill=tk.BOTH, expand=1)
## middle pane - color swatch under cursor
middle_pane = tk.Frame(pane_window, bg=WINDOW_BG_COLOR)
pane_window.add(middle_pane, pady=15)
color_swatch_frame = tk.Frame(middle_pane, height=50, width=50, relief='groove', borderwidth=2)
color_swatch_frame.pack_propagate(0) # don't shrink
color_swatch_frame.pack()
## right pane - color value labels
right_pane = tk.Frame(pane_window, bg=WINDOW_BG_COLOR)
pane_window.add(right_pane, pady=15)
## create labels to display color values
b_label = tk.Label(right_pane, text='B: ', width=5, anchor='w', bg=WINDOW_BG_COLOR)
b_label.grid(row=0, column=0, padx=(15, 0))
g_label = tk.Label(right_pane, text='G: ', width=5, anchor='w', bg=WINDOW_BG_COLOR)
g_label.grid(row=0, column=1)
r_label = tk.Label(right_pane, text='R: ', width=5, anchor='w', bg=WINDOW_BG_COLOR)
r_label.grid(row=0, column=2, padx=(0, 15))
h_label = tk.Label(right_pane, text='H: ', width=5, anchor='w', bg=WINDOW_BG_COLOR)
h_label.grid(row=1, column=0, padx=(15, 0), pady=7)
s_label = tk.Label(right_pane, text='S: ', width=5, anchor='w', bg=WINDOW_BG_COLOR)
s_label.grid(row=1, column=1, pady=7)
v_label = tk.Label(right_pane, text='V: ', width=5, anchor='w', bg=WINDOW_BG_COLOR)
v_label.grid(row=1, column=2, pady=7, padx=(0, 15))
color_value_labels = [ b_label, g_label, r_label, h_label, s_label, v_label ]
## for taking screenshots
sct = mss()
## for getting mouse pos
mouse = Controller()
root.after(0, func=lambda: update(root, sct, mouse, image_label, color_swatch_frame, color_value_labels))
root.mainloop()