forked from formazione/utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_screen_to_pdf.py
158 lines (134 loc) · 4.1 KB
/
save_screen_to_pdf.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
# grabscreen.py
import pyscreenshot as ImageGrab
import os
from PIL import Image
from io import BytesIO
import win32clipboard
import pygame
import win32api
import win32con
import win32gui
import tkinter as tk
from glob import glob
def destroy():
root.destroy()
root = tk.Tk()
b = tk.Button(root, text="Click to Capture screen",
fg='red',
bg='yellow',
font="Arial 36",
command=root.destroy)
b.pack()
root.mainloop()
def preview():
global count_image
root = tk.Tk()
image = tk.PhotoImage(file=f"{path}\\im{count_image}.png")
b = tk.Label(root, image=image,
fg='red',
bg='yellow',
font="Arial 36")
b.pack()
root.mainloop()
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
count_image = 0
path = "G:\\saved_by_save"
print(path)
def grab(x, y, w, h):
global count_image
im = ImageGrab.grab(bbox=(x, y, w, h))
if count_image < 10:
count_image = "0" + str(count_image)
else:
count_image = str(count_image)
im.save(f'{path}\\im{count_image}.png')
image = Image.open(f'{path}\\im{count_image}.png')
image.save(f'{path}\\im{count_image}.png')
pygame.init()
info = pygame.display.Info()
w = info.current_w
h = info.current_h
screen = pygame.display.set_mode((w, h), pygame.NOFRAME) # For borderless, use pygame.NOFRAME
done = False
fuchsia = (255, 0, 128) # Transparency color
# Create layered window
hwnd = pygame.display.get_wm_info()["window"]
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
# Set window transparency color
# win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY)
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 50, win32con.LWA_ALPHA)
click1 = 0
x1 = 0
y1 = 0
x2 = 0
y2 = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
# time.sleep(.1)
if click1 == 0:
x1, y1 = pygame.mouse.get_pos()
click1 = 1
elif click1 == 1:
x2, y2 = pygame.mouse.get_pos()
dx = x1 + (x2 - x1)
dy = y1 + (y2 - y1)
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_ALPHA)
grab(x1, y1, dx, dy)
click1 = 0
# Sh
# done = True
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 50, win32con.LWA_ALPHA)
x1 = 0
y1 = 0
x2 = 0
y2 = 0
#preview()
count_image = int(count_image)
count_image += 1
screen.fill((255, 255, 255)) # Transparent background
# show_text()
if click1 == 0:
mx, my = pygame.mouse.get_pos()
dx = 5
dy = 5
elif click1 == 1:
mx2, my2 = pygame.mouse.get_pos()
x2 = mx2 - x1
y2 = my2 - y1
pygame.draw.rect(screen, (0, 255, 255), pygame.Rect(mx, my, x2, y2))
pygame.display.update()
pygame.quit()
# ============= create pdf =========
files = glob("G:/saved_by_save/*.png")
# rgb.save(PDF_FILE, 'PDF', resoultion=100.0)
for f in files:
print(f)
print(f[:-4])
newname = f[:-4] + ".png"
print(newname)
os.rename(f, newname)
files = glob("G:/saved_by_save/*.png")
print(files)
iml = []
print(f"{files=}")
for img in files:
imgs = Image.open(img)
iml.append(imgs)
pdf = "G:\\saved_by_save\\ALL.pdf"
print(iml)
image = iml[0]
iml.pop(0)
image.save(pdf, "PDF" , resolution=100.0, save_all=True, append_images=iml)
os.system("start G:\\saved_by_save")