from tkinter import * from tkinter.messagebox import showinfo from tkinter import filedialog, ttk from tkinter.ttk import * import os from PIL import Image VERSIONSTRING = "1.0.3" class ProjectLiferFrame(Frame): files = list() def __init__(self, root): ttk.Frame.__init__(self, root) # define frame_actionbuttons self.frame_actionbuttons = Frame(self) photo_folder = PhotoImage(file="data/folder.png") photo_gears = PhotoImage(file="data/gears.png") self.button_choosedirectory = Button(self.frame_actionbuttons, compound=TOP, text='Verzeichnis wählen', image=photo_folder, command=self.askdirectory) self.button_choosedirectory.image = photo_folder self.button_docombine = Button(self.frame_actionbuttons, compound=TOP, text='Kombiniere Bilder', image=photo_gears, command=self.combine_pictures) self.button_docombine.image = photo_gears # define listbox and scrollbar self.label_currentdirectory = Label(self, text="Kein Ordner ausgewählt", wraplength=550, anchor=W, justify=LEFT, padding = (0, 5, 0, 0)) self.listbox = Listbox(self, selectmode=MULTIPLE) self.scrollbar = Scrollbar(self.listbox, orient=VERTICAL) # define selector_button_frame and buttons self.frame_selector_buttons = Frame(self) self.button_selectall = Button(self.frame_selector_buttons, text='Alle wählen', command=self.select_all) self.button_selectnone = Button(self.frame_selector_buttons, text='Keine wählen', command=self.select_none) # configure listbox and scrollbar self.listbox.config(yscrollcommand=self.scrollbar.set) self.scrollbar.config(command=self.listbox.yview) self.separator_bottom = Separator(self, orient=HORIZONTAL) # define progressframe self.frame_progressbar = Frame(self) # define Progresstext self.label_progressbar = Label(self.frame_progressbar, text='', width=10, anchor=W) # define ProgressBar self.progressbar = Progressbar(self.frame_progressbar) # layout frame_actionbuttons self.frame_actionbuttons.grid(row=0, column=0, rowspan=2, sticky=N, padx=5, pady=5) self.button_choosedirectory.grid(row=0, column=0, columnspan=2, sticky=N + W + E) self.button_docombine.grid(row=3, columnspan=2, sticky=N + W + E, pady=5) # layout listbox and scrollbar self.label_currentdirectory.grid(row=0, column=1, sticky=W + E, padx=5) self.listbox.columnconfigure(0, weight=1) self.listbox.rowconfigure(0, weight=1) self.listbox.grid(row=1, column=1, sticky=W + E + N + S, pady=5, padx=5) self.scrollbar.grid(column=1, sticky=N + S) # layout frame_selector_buttons self.frame_selector_buttons.grid(row=2, column=1, sticky=W + E, padx=5) self.frame_selector_buttons.columnconfigure(0, weight=1) self.frame_selector_buttons.columnconfigure(1, weight=1) self.button_selectall.grid(row=0, column=0, sticky=N + E + W, pady=5) self.button_selectnone.grid(row=0, column=1, sticky=N + E + W, pady=5) # Horizontal Bar self.separator_bottom.grid(row=3, columnspan=2, sticky=W + E) # layout progressbar and text self.frame_progressbar.columnconfigure(1, weight=1) self.frame_progressbar.grid(row=4, column=0, columnspan=2, sticky=W + E + N + S, pady=5, padx=5) self.label_progressbar.grid(row=0, column=0, sticky=W) self.progressbar.grid(row=0, column=1, sticky=W + E + N + S) # defining options for opening a directory self.dir_opt = options = {} options['initialdir'] = 'C:\\' options['mustexist'] = False options['parent'] = root options['title'] = 'Ordner auswählen' self.columnconfigure(1, weight=1) self.rowconfigure(1, weight=1) def combine_pictures(self): """Combines two pictures into one.""" items = self.listbox.curselection() items = [self.files[int(item)] for item in items] max = int(round(len(items) / 2)) current = 0 self.progressbar["value"] = current self.progressbar["maximum"] = max root.update_idletasks() items = iter(items) for i1, path in items: img1 = self.fixpicture(Image.open(path)) i2, path = next(items, (i1, path)) img2 = self.fixpicture(Image.open(path)) # resize smaller picture x1, y1 = img1.size x2, y2 = img2.size if x1 < x2: img1 = img1.resize((x2, y2)) x, y = x2, y2 else: img2 = img2.resize((x1, y1)) x, y = x1, y1 new_im = Image.new('RGB', (x + x, y)) new_im.paste(img1, (0, 0)) new_im.paste(img2, (x, 0)) imgname1 = os.path.splitext(os.path.basename(i1))[0] imgname2 = os.path.splitext(os.path.basename(i2))[0] new_im.save(self.folder + os.path.sep + '{}_and_{}.jpg'.format(imgname1, imgname2)) current += 1 self.progressbar["value"] = current self.label_progressbar["text"] = '{}/{}'.format(current, max) root.update_idletasks() self.label_progressbar["text"] = 'Done!' self.progressbar["value"] = 0 return def fixpicture(self, picture): x, y = picture.size if x > y: picture = picture.rotate(90, expand=True) x, y = y, x # fix ratio if x / 3 * 4 > y: x = int(y / 4 * 3) elif x / 3 * 4 < y: y = int(x / 3 * 4) return picture.resize((x, y)) def askdirectory(self): """Returns a selected directoryname.""" self.listbox.delete(0, END) self.files.clear() self.folder = filedialog.askdirectory(**self.dir_opt) self.label_currentdirectory['text'] = self.folder for file in os.listdir(self.folder): pathfile = os.path.join(self.folder, file) if os.path.isfile(pathfile): self.files.append((file, pathfile)) for key, value in self.files: self.listbox.insert(END, key) return def select_all(self): self.listbox.selection_set(0, END) def select_none(self): self.listbox.selection_clear(0, END) def about_popup(self): showinfo("Über", 'ProjectLifer \n Version {}'.format(VERSIONSTRING)) if __name__ == '__main__': root = Tk() root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) root.minsize(700, 500) root.title("ProjectLifer") main_frame = ProjectLiferFrame(root) main_frame.grid(row=0, column=0, sticky=(N, S, E, W)) # create menu menubar = Menu(root) file_menu = Menu(menubar, tearoff=0) file_menu.add_command(label="Beenden", command=root.quit) menubar.add_cascade(label="Datei", menu=file_menu) menubar.add_command(label="Über", command=main_frame.about_popup) root.config(menu=menubar) root.mainloop()