from Tkinter import import os sys tkFileDialog yaml import cut_images

  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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from Tkinter import *
import os, sys, tkFileDialog, yaml
import cut_images as ctm
FIELDS = ['PP_BirthDate', 'PP_BirthPlace', 'PP_Date', 'PP_Kem', 'PP_Name', \
'PP_Num', 'PP_Podr', 'PP_SecName', 'PP_Ser', 'PP_Sex', 'PP_SurName']
FDICT = {'PP_Num': 'PP_Num2', 'PP_Ser': 'PP_Ser2'}
def read_settings():
images, markup = '.', '.'
if os.path.exists('settings.yaml'):
with open('settings.yaml', 'r') as f:
cont = yaml.load(f)
if 'images' in cont:
images = cont['images']
if 'markup' in cont:
markup = cont['markup']
return images, markup
class PaddingForm():
def __init__(self, root):
body = Frame(root)
self.parent = root
body.pack(side=TOP, fill=X)
padding = Label(body, text='Padding x, y:'.ljust(20)).pack(side=LEFT)
self.x = StringVar()
self.x.set('0')
xpadding = Entry(body, textvariable=self.x, width=10).pack(side=LEFT)
self.y = StringVar()
self.y.set('0')
padding = Entry(body, textvariable=self.y, width=10).pack(side=LEFT)
def read_conf(self):
fields = {'x': self.x, 'y': self.y}
ans = {}
for field in fields:
p = fields[field].get()
try:
ans[field] = int(p)
except ValueError:
print 'Wrong padx:', p, 'Must be natural number.'
sys.exit()
return ans
def refocus(self):
self.parent.focus_force()
return 'break'
class Window():
def __init__(self):
root = Tk()
body = Frame(root)
self.parent = root
body.pack(side=TOP)
self.form = PaddingForm(body)
body.focus_force()
self.cells = IntVar()
mode_body = Frame(body, bd=2, relief='groove')
mode_body.pack(side=TOP, fill=X)
mode_label = Label(mode_body, text='Cut mode:'.ljust(20)).pack(side=LEFT)
cells_rb = Radiobutton(mode_body, text='cells'.ljust(10), variable=self.cells, \
value=1, command=self.refocus)
cells_rb.pack(side=LEFT)
fields_rb = Radiobutton(mode_body, text='fields'.ljust(10), variable=self.cells, \
value=0, command=self.refocus)
fields_rb.pack(side=LEFT)
self.cells.set(0)
fields_body = Frame(body).pack()
left_label = Label(fields_body, text='Fields:'.ljust(20), anchor='nw')
left_label.pack(side=LEFT, fill=Y)
right_label = Frame(fields_body)
right_label.pack(side=LEFT, fill=Y)
self.fields = {}
for field in FIELDS:
self.fields[field] = IntVar()
self.fields[field].set(0)
checkbutton = Checkbutton(right_label, text=field.ljust(15), \
variable=self.fields[field], justify=LEFT, \
anchor='w', command=self.refocus).pack(fill=X)
body.bind('<Return>', self.read_configuration)
root.mainloop()
def read_configuration(self, event):
conf_dict = {'cells_mode': 0, 'padx': 5, 'pady': 5, 'fields': ['PP_Ser', 'PP_Ser2', \
'PP_SerSec', 'PP_Ser2Sec', 'PP_Num', 'PP_Num2']}
fields = []
for field in self.fields:
if self.fields[field].get():
fields.append(field)
if field in FDICT:
fields.append(FDICT[field])
if len(fields) > 0:
print len(fields), 'fields were chosen.'
conf_dict['fields'] = fields
if self.cells.get():
conf_dict['cells_mode'] = 1
else:
conf_dict['cells_mode'] = 0
padding = self.form.read_conf()
for key in padding:
conf_dict['pad' + key] = padding[key]
self.data = conf_dict
self.parent.destroy()
def refocus(self):
self.form.refocus()
class Bienvenido():
def __init__(self, images, markup, output):
root = Tk()
root.geometry('+%d+%d' % (10, 10))
self.parent = root
self.window = Frame(root)
self.window.grid(row = 0, column = 0)
self.message = Label(self.window, text = 'Please, enter paths.')
self.message.grid(row = 0, column = 0, columnspan = 2)
self.images_message = Label(self.window, text = 'Images directory:')
self.images_message.grid(row = 1, column = 0)
self.images_entry = Entry(self.window, width = 80)
self.images_entry.grid(row = 1, column = 1)
self.images_entry.insert(0, images)
self.images_view = Button(self.window, text = 'View', command = self.get_images_dir, width = 12)
self.images_view.grid(row = 1, column = 2)
self.markup_message = Label(self.window, text = 'Markup directory:')
self.markup_message.grid(row = 2, column = 0)
self.markup_entry = Entry(self.window, width = 80)
self.markup_entry.grid(row = 2, column = 1)
self.markup_entry.insert(0, markup)
self.markup_view = Button(self.window, text = 'View', command = self.get_markup_dir, width = 12)
self.markup_view.grid(row = 2, column = 2)
self.output_message = Label(self.window, text = 'Output file:')
self.output_message.grid(row = 3, column = 0)
self.output_entry = Entry(self.window, width = 80)
self.output_entry.grid(row = 3, column = 1)
self.output_entry.insert(0, output)
self.ok_button = Button(self.window, text = 'Ok', command = self.acception, width = 12)
self.ok_button.grid(row = 4, column = 1)
root.mainloop()
def get_images_dir(self):
path = self.images_entry.get()
w = SettingsWindow(Toplevel(self.parent), path, 'Choose images directory')
self.images_entry.delete(0, END)
self.images_entry.insert(0, w.dir)
def get_markup_dir(self):
path = self.markup_entry.get()
w = SettingsWindow(Toplevel(self.parent), path, 'Choose markup directory')
self.markup_entry.delete(0, END)
self.markup_entry.insert(0, w.dir)
def acception(self):
self.markup = self.markup_entry.get().replace(' ', '')
self.images = self.images_entry.get().replace(' ', '')
self.out = self.output_entry.get().replace(' ', '')
self.parent.destroy()
class SettingsWindow:
def __init__(self, root, path, message):
root.attributes("-alpha", 0.0)
root.geometry('+%d+%d' % (10, 10))
self.dir_opt = options = {}
options['initialdir'] = path
options['mustexist'] = False
options['parent'] = root
options['title'] = 'Select an image'
self.dir = tkFileDialog.askdirectory(**self.dir_opt)
root.destroy()
class FolderSelection():
def __init__(self, images):
root = Tk()
root.geometry('+%d+%d' % (10, 10))
self.parent = root
self.navigation_window = Frame(root)
self.navigation_window.pack()
self.list = os.listdir(images)
self.select_all_var = IntVar()
self.select_all = Checkbutton(self.navigation_window,
variable = self.select_all_var, text = ('%s' %'Select all/Clear all').ljust(40),
width = 22, command = self.select_clear).pack(side = LEFT,fill = X)
self.list_cont = Frame(self.navigation_window).pack(side = BOTTOM, fill = X)
self.scrollbar = Scrollbar(self.list_cont)
self.scrollbar.pack( side = RIGHT, fill = Y )
self.path_list = Listbox(self.list_cont, width = 40, height = 50,
selectmode = MULTIPLE, yscrollcommand = self.scrollbar.set)
self.path_list.pack(side = RIGHT, fill = BOTH)
self.path_list.bind('<Return>', self.get_ideals_list)
self.scrollbar.config( command = self.path_list.yview )
for line in self.list:
self.path_list.insert(END, line.replace('\\', '/'))
self.ok_button = Button(self.navigation_window, text = 'Confirm',
width = 10, command = self.get_ideals_list).pack(side = RIGHT)
root.mainloop()
def select_clear(self):
if self.select_all_var.get():
self.path_list.selection_set(0, END)
else:
self.path_list.selection_clear(0, END)
self.path_list.focus_force()
return 'break'
def get_ideals_list(self, event = False):
lista = self.get_lines(self.path_list.curselection())
self.list = lista
self.parent.destroy()
def get_lines(self, index_set):
lines = []
for i in index_set:
lines.append(self.path_list.get(i))
return lines
images, markup = read_settings()
b = Bienvenido(images, markup, '.')
fs = FolderSelection(b.images)
w = Window()
d = w.data
ctm.main(b.images, b.markup, b.out, d['fields'], fs.list, d['cells_mode'], [d['padx'], d['pady']])