-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.py
More file actions
171 lines (130 loc) · 5.98 KB
/
Copy pathselect.py
File metadata and controls
171 lines (130 loc) · 5.98 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import tkinter as tk
from tkinter import *
import tkinter.ttk as ttk
import shutil
import os
class NthValueOutOfRange(Exception):
def __init__(self,msg):
super().__init__(msg)
class SelectPage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
global label_cropped_folder
global label_selected_folder
global entry_nth_number
global progress_select
global label_progress_select
font_page_title=('Helvetica',16,'bold')
font_label=("Verdana",10)
croppedPath=""
selectedPath=""
label=tk.Label(self,text="Select Page",font=font_page_title)
label_cropped_folder=Label(self,
text="Cropped Image Directory ",
width=55, height=4,
font=font_label,
wraplength=500,
justify='left',
anchor=W,
fg='grey')
button_cropped_explorer=ttk.Button(self,
text="Browse",
command=lambda:self.browseFiles(label_cropped_folder))
label_selected_folder=Label(self,
text="Selected Image Directory ",
width=55, height=4,
font=font_label,
wraplength=500,
justify='left',
anchor=W,
fg='grey')
button_selected_explorer=ttk.Button(self,
text="Browse",
command=lambda:self.browseFiles(label_selected_folder))
label_nth_number=Label(self,text="Nth number: ",font=font_label)
entry_nth_number=ttk.Entry(self,width=5)
label_progress_select=Label(self,text="Standby",fg='orange')
progress_select=ttk.Progressbar(self,orient='horizontal',length=100,mode='determinate')
button_select_execute=ttk.Button(self,text='Select',
command=self.selectFunction)
label.grid(column=0,row=0)
label_cropped_folder.grid(sticky=W,column=1,row=1,columnspan=10)
button_cropped_explorer.grid(column=12,row=1)
label_selected_folder.grid(sticky=W,column=1,row=2,columnspan=10)
button_selected_explorer.grid(column=12,row=2)
label_nth_number.grid(column=1,row=3,sticky=W)
entry_nth_number.grid(column=2,row=3)
label_progress_select.grid(column=1,row=4)
progress_select.grid(column=1,row=5)
button_select_execute.grid(column=10,row=6)
def browseFiles(self,label):
directory=filedialog.askdirectory(initialdir='/',title="Select a directory")
if label==label_cropped_folder:
#try block for cropped directory
try:
if not any(fname.endswith('.JPG') for fname in os.listdir(directory)):
raise Exception("Directory has no .jpeg files")
except Exception:
messagebox.showerror("Select A Directory","JPG files(s) not found")
return
label.configure(text="Cropped Folder: "+ directory, fg='blue')
self.croppedPath=directory
else:
label.configure(text="Selected Folder: "+ directory, fg='blue')
self.selectedPath=directory
label_progress_select.configure(text="Standby",fg='orange')
if (directory==""):
label.configure(text="No Folder Selected ", fg='red')
raise Exception("Empty directory")
def selectFunction(self):
#try block for cropped image path
try:
print(self.croppedPath)
if (self.croppedPath==""):
raise Exception("Empty Cropped Directory")
except AttributeError:
print("Empty Cropped Directory")
messagebox.showerror("Select A Directory","Empty Cropped Directory")
return
except Exception:
print("Empty Cropped Directory")
messagebox.showerror("Select A Directory","Empty Cropped Directory")
return
#try block for selected image path
try:
print(self.selectedPath)
if (self.selectedPath==""):
raise Exception("Empty Cropped Directory")
except AttributeError:
print("Empty Selected Directory")
messagebox.showerror("Select A Directory","Empty Selected Directory")
return
except Exception:
print("Empty Selected Directory")
messagebox.showerror("Select A Directory","Empty Selected Directory")
return
#try block for nth number
try:
if(int(entry_nth_number.get())<=1 or int(entry_nth_number.get())>=len(os.listdir(self.croppedPath)) ):
raise NthValueOutOfRange("Nth Value Out of Range")
except NthValueOutOfRange:
messagebox.showerror("Nth Value Input","Nth Value out of range.\nMust be between 2 and length of cropped folder")
return
except ValueError:
messagebox.showerror("Nth Value Input","Fill in Nth Value with a number")
return
label_progress_select.configure(text="PROCESSING",fg='red')
#print(self.croppedPath)
nthNum=int(entry_nth_number.get())
list=os.listdir(self.croppedPath)
progress_select['value']=0
progress_select['maximum']=len(list)//nthNum
for i in list[::nthNum]:
if not(i.endswith('.JPG')):
progress_select['value']+=1
progress_select.update()
continue
shutil.copy(self.croppedPath+'/'+i,self.selectedPath)
progress_select['value']+=1
progress_select.update()
label_progress_select.configure(text="DONE",fg='green')