-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui.py
122 lines (94 loc) · 4.07 KB
/
gui.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
import customtkinter as ctk
import os
import sys
import time
from PIL import Image
from main import set_tesseract_path, select_region_and_capture, ocr_image
# TODO: main frame: take screenshot, import image, settings (language, parameters, shortcuts, etc.)
# TODO: screenshot on release checkbox
# TODO: tab1: screenshot and ocr image with scroll bar
# TODO: tab2: imported image and ocr image with scroll bar
# TODO: tab3: text result box
ctk.set_appearance_mode("dark")
class Tab(ctk.CTkTabview):
def __init__(self, parent, **kwargs) -> None:
super().__init__(parent, **kwargs)
self.parent = parent
self.input_image = None
self.output_image = None
self.add("Tab 1")
self.tab_1 = ctk.CTkScrollableFrame(master=self.tab("Tab 1"))
self.tab_1.pack(fill="both", expand=True)
self.input_image_text = ctk.CTkLabel(self.tab_1, text="")
self.input_image_text.grid(row=0, column=0, padx=20, pady=10)
self.input_image_label = ctk.CTkLabel(
self.tab_1, text="", image=self.input_image)
self.input_image_label.grid(
row=1, column=0, padx=20, pady=10, sticky="nsew")
self.output_image_text = ctk.CTkLabel(
self.tab_1, text="")
self.output_image_text.grid(row=2, column=0, padx=20, pady=10)
self.output_image_label = ctk.CTkLabel(
self.tab_1, text="", image=self.output_image)
self.output_image_label.grid(
row=3, column=0, padx=20, pady=10, sticky="nsew")
# Configure row and column weights
self.tab_1.grid_rowconfigure(3, weight=1)
self.tab_1.grid_columnconfigure(0, weight=1)
self.add("Tab 2")
class App(ctk.CTk):
def __init__(self) -> None:
super().__init__()
self.title("image_example.py")
self.geometry("800x800")
self.image_scale_factor = 1
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(0, weight=1)
self.take_screenshot_button = ctk.CTkButton(
self, text="Take Screenshot", command=self.take_screen_shot)
self.take_screenshot_button.grid(
row=0, column=0, padx=20, pady=10)
self.tabview = Tab(self)
self.tabview.grid(row=1, column=0, padx=20, pady=10, sticky="nsew")
self.image_path = os.path.join(os.path.dirname(
os.path.realpath(__file__)), "ocr_images")
def take_screen_shot(self) -> None:
# Minimize window when pressing the button
self.iconify()
time.sleep(0.5)
try:
set_tesseract_path()
except (RuntimeError, FileNotFoundError) as e:
print(e)
sys.exit(1)
coords = {"ix": -1, "iy": -1, "x_end": -
1, "y_end": -1, "drawing": False}
screenshot_region = select_region_and_capture(coords)
result = ocr_image(
screenshot_region,
preprocess_params={
"size": (screenshot_region.width * 4, screenshot_region.height * 4)
},
)
print(result)
screenshot_image = Image.frombytes(
"RGB", (screenshot_region.width, screenshot_region.height), screenshot_region.tobytes())
image_size = (int(screenshot_region.width * self.image_scale_factor),
int(screenshot_region.height * self.image_scale_factor))
self.input_image = ctk.CTkImage(screenshot_image, size=(
image_size[0], image_size[1]))
self.tabview.input_image_label.configure(image=self.input_image)
processed_image = Image.open(
os.path.join(self.image_path, "preprocessed.png"))
self.output_image = ctk.CTkImage(processed_image, size=(
image_size[0], image_size[1]))
self.tabview.output_image_label.configure(image=self.output_image)
self.tabview.input_image_text.configure(
text=f"Input Image")
self.tabview.output_image_text.configure(
text=f"Output Image")
# Maximize window when done
self.deiconify()
if __name__ == "__main__":
app = App()
app.mainloop()