/
Program.py
177 lines (143 loc) · 4.16 KB
/
Program.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
from tkinter import (
Tk,
Menu,
filedialog,
messagebox,
PhotoImage,
Label,
Button,
Frame,
Scrollbar,
Text,
END,
)
from PIL import ImageTk, Image
import os
import pytesseract
def newProcess():
imgPanel = Label(root, image="")
imgPanel.pack()
content_text.delete(1.0, END)
def openFile():
filename = filedialog.askopenfilename(
defaultextension=".jpg",
title="Buka Gambar",
filetypes=[("All Files", "*.*"), ("Images", "*.jpg, *.png")],
)
if filename:
global file_name
file_name = filename
root.title("{} - {}".format(os.path.basename(file_name), Program_Title))
pytesseract.pytesseract.tesseract_cmd = r"tesseract"
ocr = pytesseract.image_to_string(filename)
content_text.delete(1.0, END)
content_text.insert(END, ocr)
return filename
def openImage(event=None):
x = openFile()
img = Image.open(x)
img = img.resize((250, 250), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
imgPanel.config(image=img)
imgPanel.image = img
imgPanel.pack()
def saveFileAs(event=None):
global content_text
t = content_text.get("1.0", "end-1c")
saveName = filedialog.asksaveasfilename(
defaultextension=".txt",
title="Simpan Hasil OCR",
initialfile="Hasil OCR",
filetypes=[("Text Document", "*.txt"), ("All Files", "*.*")],
)
file1 = open(saveName, "w+")
file1.write(t)
def aboutInfo(event=None):
messagebox.showinfo(
"Tentang Saya",
"{}{}".format(Program_Title, "\nNama: Muhammad Ammar Fakhri\nNIM: 41516010027"),
)
def helpInfo(event=None):
messagebox.showinfo(
"Klik di sini", "Saya mengerjakan program ini sendiri tanpa bantuan orang lain"
)
def exitProgram(event=None):
if messagebox.askokcancel("Keluar?", "Yakin ingin keluar?"):
root.destroy()
# # # #
# Root configuration
Program_Title = "OCR Viewer"
file_name = None
root = Tk()
root.geometry("800x600")
root.title(Program_Title)
menu_bar = Menu(root) # Menampilkan menu bar
# Menu bar icon
new_icon = PhotoImage(file="icons/new.png")
open_icon = PhotoImage(file="icons/open.png")
save_icon = PhotoImage(file="icons/save.png")
exit_icon = PhotoImage(file="icons/exit.png")
about_icon = PhotoImage(file="icons/about.png")
help_icon = PhotoImage(file="icons/help.png")
# Function untuk mengonstruksi menu bar
def menuBarConstruct():
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(
label="Baru",
compound="left",
image=new_icon,
command=newProcess,
)
file_menu.add_command(
label="Buka Gambar",
compound="left",
image=open_icon,
command=openImage,
)
file_menu.add_command(
label="Simpan Hasil OCR",
compound="left",
image=save_icon,
command=saveFileAs,
)
file_menu.add_separator()
file_menu.add_command(
label="Keluar",
compound="left",
image=exit_icon,
command=exitProgram,
)
menu_bar.add_cascade(label="File", menu=file_menu)
about_menu = Menu(menu_bar, tearoff=0)
about_menu.add_command(
label="Tentang saya",
compound="left",
image=about_icon,
command=aboutInfo
)
about_menu.add_command(
label="Klik di sini",
compound="left",
image=help_icon,
command=helpInfo
)
menu_bar.add_cascade(label="Tentang", menu=about_menu)
menuBarConstruct() # Untuk menampilkan menu bar
root.config(menu=menu_bar)
lbl_welcome = Label(root, text="Selamat datang di program pembaca teks dari gambar")
btn_input = Button(root, text="Masukkan gambar", command=openImage)
lbl_welcome.config(font=("Arial", 20))
lbl_welcome.pack()
btn_input.config(font=("Arial", 12))
btn_input.pack()
imgPanel = Label(root, image="")
imgPanel.pack()
lbl_OCR = Label(root, text="")
lbl_OCR.pack()
content_text = Text(root, wrap="word", undo=1)
content_text.pack(expand="yes", fill="both")
scroll_bar = Scrollbar(content_text)
content_text.configure(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=content_text.yview)
scroll_bar.pack(side="right", fill="y")
root.mainloop()