-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmwphase5.py
More file actions
151 lines (109 loc) · 4.73 KB
/
mwphase5.py
File metadata and controls
151 lines (109 loc) · 4.73 KB
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
from tkinter import *
import PyPDF2
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfile
from functions import display_logo, display_textbox, extract_images, display_icon, display_images
page_contents = []
all_images = []
img_idx = [0]
displayed_img = []
def right_arrow(all_images, current_img, what_text):
if img_idx[-1] < len(all_images) -1:
new_idx = img_idx[-1] + 1
img_idx.pop()
img_idx.append(new_idx)
if displayed_img:
displayed_img[-1].grid_forget()
displayed_img.pop()
new_img = all_images[img_idx[-1]]
current_img = display_images(new_img)
displayed_img.append(current_img)
what_text.set("image " + str(img_idx[-1] + 1) + " out of " + str(len(all_images)))
def left_arrow(all_images, current_img, what_text):
if img_idx[-1] >= 1:
new_idx = img_idx[-1] - 1
img_idx.pop()
img_idx.append(new_idx)
if displayed_img:
displayed_img[-1].grid_forget()
displayed_img.pop()
new_img = all_images[img_idx[-1]]
current_img = display_images(new_img)
displayed_img.append(current_img)
what_text.set("image " + str(img_idx[-1] + 1) + " out of " + str(len(all_images)))
def copy_text(content):
root.clipboard_clear()
root.clipboard_append(content[-1])
def save_all(images):
counter = 1
for i in images:
if i.mode != "RGB":
i = i.convert("RG")
i.save("image" + str(counter) + ".png", format="png")
counter += 1
def save_image(i):
if i.mode != "RGB":
i = i.convert("RGB")
i.save("img.png", format="png")
root = Tk()
root.geometry('+%d+%d'%(350,10)) #place GUI at x=350, y=10
#header area - logo & browse button
header = Frame(root, width=800, height=175, bg="white")
header.grid(columnspan=3, rowspan=2, row=0)
#main content area - text and image extraction
main_content = Frame(root, width=800, height=250, bg="#003A63")
main_content.grid(columnspan=3, rowspan=2, row=4)
def open_file():
for i in img_idx:
img_idx.pop()
img_idx.append(0)
browse_text.set("loading...")
file = askopenfile(parent=root, mode='rb', filetypes=[("Pdf file", "*.pdf")])
if file:
read_pdf = PyPDF2.PdfFileReader(file)
page = read_pdf.getPage(0)
page_content = page.extractText()
#page_content = page_content.encode('cp1252')
page_content = page_content.replace('\u2122', "'")
page_contents.append(page_content)
if displayed_img:
displayed_img[-1].grid_forget()
displayed_img.pop()
for i in range(0, len(all_images)):
all_images.pop()
images = extract_images(page)
for i in images:
all_images.append(i)
img = images[img_idx[-1]]
current_image = display_images(img)
displayed_img.append(current_image)
#show text box on row 4 col 0
display_textbox(page_content, 4, 0, root)
#reset the button text back to Browse
browse_text.set("Browse")
img_menu = Frame(root, width=800, height=175)
img_menu.grid(columnspan=3, rowspan=1, row=2)
what_text = StringVar()
what_img = Label(root, textvariable=what_text, font=("shanti", 10))
what_text.set("image " + str(img_idx[-1] + 1) + " out of " + str(len(all_images)))
what_img.grid(row=2, column=1)
display_icon('arrow_l.png', 2, 0, E, lambda:left_arrow(all_images, current_image, what_text))
display_icon('arrow_r.png', 2, 2, W, lambda:right_arrow(all_images, current_image, what_text))
save_img = Frame(root, width=800, height=175, bg="#c8c8c8")
save_img.grid(columnspan=3, rowspan=1, row=3)
copyText_btn = Button(root, text="copy text",command=lambda:copy_text(page_contents), font=("shanti", 10), height=1, width=15)
saveAll_btn = Button(root, text="save all images",command=lambda:save_all(all_images), font=("shanti", 10), height=1, width=15)
save_btn = Button(root, text="save image", command=lambda:save_image(all_images[img_idx[-1]]), font=("shanti", 10), height=1, width=15)
copyText_btn.grid(row=3,column=0)
saveAll_btn.grid(row=3,column=1)
save_btn.grid(row=3,column=2)
display_logo('HUTASKREAL.png', 0, 0)
#instructions
instructions = Label(root, text="Select a PDF file", font=("Raleway", 10), bg="white")
instructions.grid(column=2, row=0, sticky=SE, padx=75, pady=5)
#browse button
browse_text = StringVar()
browse_btn = Button(root, textvariable=browse_text, command=lambda:open_file(), font=("Raleway",12), bg="#003A63", fg="white", height=1, width=15)
browse_text.set("Browse")
browse_btn.grid(column=2, row=1, sticky=NE, padx=50)
root.mainloop()