-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnipe.py
285 lines (238 loc) · 10.9 KB
/
cnipe.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
"""
Take beautiful screenshots from CNIPE
@author : Ashutosh | created on : 01-02-2023
"""
import os
import random
import tempfile
from tkinter import filedialog, messagebox
import customtkinter
from PyQt5 import QtWidgets
from PIL import Image, ImageOps
from src.screenshot_taker import MyWidget
from src.html_to_image import make_gradient_image
from src.image_shaper import ImgModifier
from src.scroll_action import ScrollableLabelButtonFrame
customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark
customtkinter.set_default_color_theme("green") # Theme
UI_PATH = "ui"
tempdir = tempfile.TemporaryDirectory()
# save gradient and captured images in temp file which gets deleted after the execution is done
IMAGE_DIR_PATH = tempdir.name
class App(customtkinter.CTk):
"""
Tkinter Class
"""
def __init__(self):
super().__init__()
self._home_background = "#242424"
# set full screen for better view, full screen is different in different OS
if os.name == "nt":
self.state('zoomed')
elif os.name == "posix":
self.attributes('-fullscreen', True)
self.wm_iconbitmap(os.path.join("ui", "gnapper.ico"))
self.iconbitmap(default=r'ui\\gnapper.ico')
self.title("Cnipe - Take Beautiful Screenshot")
self.home_image = customtkinter.CTkImage(light_image=Image.open(os.path.join(UI_PATH, "home_dark.png")),
dark_image=Image.open(os.path.join(UI_PATH, "home_light.png")),
size=(20, 20))
self.use_same_colors = None
self.colors = [(247, 253, 166, 1), (128, 255, 221, 1), (255, 128, 249, 1)] # initialise with set of colors
self.logo_image = customtkinter.CTkImage(
Image.open(os.path.join(UI_PATH, "gnapper.png")), size=(30, 30))
self.main_image = None
self.modified_image = None
self.padding = 25
self.scrolls = None
self.selected_grad_image = None
# set grid layout 1x2
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
# create navigation frame
self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)
self.navigation_frame.grid(row=0, column=0, sticky="nsew")
self.navigation_frame.grid_rowconfigure(4, weight=1)
self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text=" CNIPE ",
image=self.logo_image,
compound="left",
font=customtkinter.CTkFont(size=15, weight="bold"))
self.navigation_frame_label.grid(row=0, column=0, padx=40, pady=40)
# place button
self.sc_button = customtkinter.CTkButton(
self.navigation_frame,
text="Capture Screenshot", command=self.capture_screenshot, border_spacing=10)
self.sc_button.grid(row=1, column=0, sticky="ew", padx=20, pady=20)
# frames
self.home_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10,
text="Home",
fg_color="transparent", text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
image=self.home_image, anchor="w", command=self.home_button_event)
self.home_button.grid(row=2, column=0, sticky="ew")
# create home frame
self.home_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
self.home_frame.grid_columnconfigure(0, weight=1)
self.home_button_event()
def capture_screenshot(self, use_old=False, new_gradient=None, padding=25):
"""
capture screenshot
"""
capture_name = "capture.png"
gradient_name = "gradient.png"
if not use_old:
# minimise the app after clicking screenshot button
self.wm_state('iconic')
self.clear_frame(name="home")
qt_app = QtWidgets.QApplication([])
window = MyWidget(capture_path=os.path.join(IMAGE_DIR_PATH, capture_name))
window.show()
qt_app.aboutToQuit.connect(qt_app.deleteLater)
qt_app.exec_()
window.destroy()
if new_gradient is None:
color_grad = None
if self.use_same_colors is not None and self.use_same_colors.get() == 1:
color_grad = self.colors
# create gradient
self.selected_grad_image, self.colors = make_gradient_image(
store_dir=IMAGE_DIR_PATH,
gradient_name=gradient_name,
colors=color_grad
)
else:
self.selected_grad_image = new_gradient
image_modify_object = ImgModifier(
outer_img=Image.open(os.path.join(self.selected_grad_image)),
inner_img=Image.open(os.path.join(IMAGE_DIR_PATH, capture_name)),
padding=padding
)
self.modified_image = image_modify_object.paste_img(rounded_corners=16)
self.modified_image.save(os.path.join(IMAGE_DIR_PATH, "modified.png"))
pad_x, pad_y = 60, 40
x, y = self.home_frame._current_width, self.home_frame._current_height # noqa
x -= (8 * pad_x)
y -= (10 * pad_y)
bg_image = ImageOps.contain(self.modified_image, (int(x), int(y)))
# show image in home frame
bg_image = customtkinter.CTkImage(bg_image, size=(bg_image.size[0], bg_image.size[1]))
if self.main_image is not None:
try:
self.main_image.destroy()
except ValueError:
pass
self.main_image = customtkinter.CTkLabel(self.home_frame, text="", image=bg_image)
self.main_image.grid(row=0, column=0, padx=pad_x, pady=pad_y)
# refresh gradient, generate random gradient
button_frame = customtkinter.CTkFrame(self.home_frame, fg_color=self._home_background)
regen_grad = customtkinter.CTkButton(
button_frame,
text="Random Gradient", command=self.generate_gradient, border_spacing=10,
fg_color=self._home_background, border_color="#e337b1", border_width=2
)
regen_grad.grid(row=0, column=0, padx=10, pady=10)
button_frame.grid(row=2, column=0)
# save modified image
save_button = customtkinter.CTkButton(
button_frame, text="Save Image", command=self.save_image, border_spacing=10,
fg_color=self._home_background, border_color="#308eea", border_width=2)
save_button.grid(row=0, column=1, padx=10)
# checkbox for same color
if self.use_same_colors is None:
self.use_same_colors = customtkinter.CTkCheckBox(
button_frame, text="Keep Same Colors"
)
self.use_same_colors.grid(row=0, column=2, padx=10, pady=10)
else:
current_val = self.use_same_colors.get()
self.use_same_colors = customtkinter.CTkCheckBox(
button_frame, text="Keep Same Colors"
)
self.use_same_colors.grid(row=0, column=3, padx=10, pady=10)
if current_val == 1:
self.use_same_colors.select()
# slider frame
sl_frame = customtkinter.CTkFrame(button_frame, fg_color=self._home_background)
slider = customtkinter.CTkSlider(
sl_frame, from_=5, to=100, command=self.slider_event,
number_of_steps=15, fg_color="#49cdc5"
)
self.padding = padding
slider.set(self.padding)
slider_label = customtkinter.CTkLabel(sl_frame, text=f"Padding : {padding}x")
slider_label.grid(row=0, column=0, padx=10)
slider.grid(row=0, column=1, padx=10)
sl_frame.grid(row=0, column=4)
# scrolls gradients
if self.scrolls is None or not use_old:
self.scrolls = ScrollableLabelButtonFrame(
master=self.home_frame, width=200, height=170,
command=self.action_taken, corner_radius=10, orientation="horizontal",
fg_color=self._home_background
)
self.scrolls.grid(row=5, column=0, padx=100, pady=10, sticky="nsew")
grad_images_dir = r"ui/premade_gradients"
gradients = [grad_images_dir + "/" + i for i in os.listdir(grad_images_dir)]
gradients = random.sample(gradients, 20) # get 20 random gradients
for i in range(len(gradients)):
self.scrolls.add_item(
gradients[i], image=customtkinter.CTkImage(
Image.open(gradients[i]), size=(100, 70)
)
)
self.deiconify()
def select_frame_by_name(self, name):
"""
Select frame on main window by name
"""
# set button color for selected button
self.home_button.configure(fg_color=("gray75", "gray25") if name == "home" else "transparent")
# show selected frame
if name == "home":
self.home_frame.grid(row=0, column=1, sticky="nsew")
else:
self.home_frame.grid_forget()
def home_button_event(self):
"""Select home frame"""
self.select_frame_by_name("home")
def clear_frame(self, name):
"""
name
"""
if name == "home":
# destroy all widgets from frame
for widget in self.home_frame.winfo_children():
widget.destroy()
# this will clear frame and frame will be empty
# if you want to hide the empty panel then
# frame.pack_forget()
def generate_gradient(self):
"""Generate random gradient"""
self.capture_screenshot(use_old=True, padding=self.padding)
def slider_event(self, slide):
"""
Slider padding
"""
self.capture_screenshot(use_old=True, padding=int(slide), new_gradient=self.selected_grad_image)
def save_image(self):
"""save image to selected file"""
file_path = filedialog.asksaveasfilename(
defaultextension=".png", filetypes=[("PNG", "*.png")]
)
if file_path:
self.modified_image.save(file_path)
def on_closing(self):
"""On close"""
if messagebox.askokcancel("Quit", "Do you want to quit, have you saved the image?"):
tempdir.cleanup()
self.destroy()
def action_taken(self, image):
"""Action after click"""
self.capture_screenshot(
use_old=True, new_gradient=image
)
if __name__ == "__main__":
app = App()
app.after(0, lambda: app.state('zoomed'))
app.protocol("WM_DELETE_WINDOW", app.on_closing)
app.mainloop()