-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
213 lines (122 loc) · 6.01 KB
/
main.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
import os
import logging
import shutil
import re
import PySimpleGUI as sg
from PIL import Image
#? You can use RIPSM Chromium to download comics/manga from TMO or other sources
numbers = re.compile(r'(\d+)')
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
#*------- INTERFACE INITIALIZATION -----------------------
sg.theme('DarkAmber')
font = ('Arial', 13)
layout = [[sg.Button('OrganizePics', key="organize_pics",size=(20,10),pad=(0,50)),sg.Button('ImageToPdf', key="convert_to_pdf",size=(20,10))],
[sg.Button('Exit', key="exit",size=(10,2))]]
window = sg.Window('ImageToPdf', layout,
icon="media\logoImageToPdf.ico", size=(800, 600), font=font,element_justification='c')
#*------------ GLOBAL VARIABLES -----------
folder = None
foldername = None
folder_convertToPdf = None # * Put here the folder path converted
foldername_convertToPdf = None
folder_generated = 0 # * stores if we have create the ImageToPdf folder before
#*------------ LOGGING -----------
logging.basicConfig(level=logging.WARNING,
format='[%(asctime)s.%(msecs)03d] [%(levelname)-7s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
#*------
def organizePics():
global folder_generated
# Create output folder
if not os.path.isdir(foldername+"/"+"ImageToPdf"):
os.mkdir(foldername+"/"+"ImageToPdf")
else: # delete contents of folder if it exists
for f in os.listdir(foldername+"/"+"ImageToPdf"):
os.remove(os.path.join(foldername+"/"+"ImageToPdf", f))
imageToPdf_folder_name = foldername+"/"+"ImageToPdf"
current_page = 0 # stores the number of files for renaming pics in order
# iterating trough the subfolders
for filename in sorted(os.listdir(foldername), key=numericalSort):
if filename != "ImageToPdf":
absolute_path_file = foldername+filename
if os.path.isdir(absolute_path_file): # only considering subfolders
folder_nested = os.fsencode(absolute_path_file)
logging.debug(absolute_path_file)
for file_nested in os.listdir(folder_nested):
file_nested_name = "\\"+file_nested.decode()
absolute_path_file_nest = absolute_path_file+file_nested_name
logging.debug(absolute_path_file_nest)
if ".png" in absolute_path_file_nest:
logging.debug(absolute_path_file_nest)
shutil.copyfile(
absolute_path_file_nest, imageToPdf_folder_name+"\\"+str(current_page)+".png")
current_page += 1
elif ".jpg" in absolute_path_file_nest:
logging.debug(absolute_path_file_nest)
shutil.copyfile(
absolute_path_file_nest, imageToPdf_folder_name+"\\"+str(current_page)+".jpg")
current_page += 1
folder_generated = 1
return
def convertToPdf():
first_flag = 0
pics = []
if not os.path.isdir(foldername_convertToPdf):
logging.warning(
"ImageToPdf folder not generated")
return
for filename in sorted(os.listdir(foldername_convertToPdf), key=numericalSort):
absolute_path = foldername_convertToPdf+"\\"+filename
logging.debug(filename)
if first_flag == 0:
first_flag += 1
image_1 = Image.open(absolute_path)
im_1 = image_1.convert('RGB')
else:
image = Image.open(absolute_path)
im = image.convert('RGB')
pics.append(im)
im_1.save(foldername+"ImageToPdf.pdf", save_all=True, append_images=pics)
if __name__ == "__main__":
while True:
event, values = window.read()
#* MENU SCREEN
if event == "organize_pics":
window2 = sg.Window('ImageToPdf', [[sg.Text('Enter the folder that stores the image folders:', key="texto", pad=(50, 50))],
[
sg.Input(), sg.FolderBrowse('Browse'), ],
[sg.Submit(button_text="Organize", key="seleccionar_ruta"), sg.Cancel(button_text="Back", key="main", pad=(50, 50))]], icon="media\logoImageToPdf.ico", size=(800, 600), font=font, element_justification='c')
window.Close()
window = window2
elif event == "convert_to_pdf" and folder_generated == 0:
sg.Popup(
'First orgnize the folders with OrganizePics in order to generate the PDF', keep_on_top=True)
elif event == "convert_to_pdf" and folder_generated == 1:
convertToPdf()
sg.Popup(
f"Pdf generated in folder {foldername_convertToPdf}", keep_on_top=True)
break
#* ORGANIZE PICS SCREEN
elif event == "seleccionar_ruta":
path = values['Browse']+"/"
# * Put here the folder path that contains all the folders with the pictures
folder = os.fsencode(path)
foldername = folder.decode()
folder_convertToPdf = os.fsencode(foldername+"ImageToPdf/")
foldername_convertToPdf = folder_convertToPdf.decode()
if foldername_convertToPdf!="/ImageToPdf/":
organizePics()
sg.Popup('The files have been organized', keep_on_top=True)
else:
sg.Popup('Please introduce a folder', keep_on_top=True)
elif event == "main":
window2 = sg.Window('ImageToPdf', [[sg.Button('OrganizePics', key="organize_pics",size=(20,10),pad=(0,50)),sg.Button('ImageToPdf', key="convert_to_pdf",size=(20,10))],
[sg.Button('Exit', key="exit",size=(10,2))]], icon="media\logoImageToPdf.ico", size=(800, 600), font=font,element_justification='c')
window.Close()
window = window2
elif event == sg.WIN_CLOSED or event == 'exit':
break
window.close()