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']])