from tkinter import * from tkinter import filedialog, ttk from tkinter.ttk import * import os from PIL import Image class ProjectLiferFrame(Frame): files = list() def __init__(self, root): ttk.Frame.__init__(self, root, padding=(5, 5, 5, 5)) # define buttons self.butcombine = Button(self, text='Kombiniere Bilder', command=self.combine_pictures) self.butdirchooser = Button(self, text='Verzeichnis wählen', command=self.askdirectory) self.butselall = Button(self, text='Alle wählen', command=self.select_all) self.butselnone = Button(self, text='Keine wählen', command=self.select_none) # define ProgressBar self.pbar = Progressbar(self) self.listbox = Listbox(self, selectmode=MULTIPLE) self.scrollbar = Scrollbar(self.listbox, orient=VERTICAL) self.listbox.config(yscrollcommand=self.scrollbar.set) self.scrollbar.config(command=self.listbox.yview) self.butcombine.grid(row=0, rowspan=2, columnspan=2, sticky=W + E + N + S, padx=5, pady=5) self.butdirchooser.grid(row=2, columnspan=2, padx=5, pady=5) self.butselall.grid(row=3, column=0) self.butselnone.grid(row=3, column=1) self.pbar.grid(row=4, columnspan=3, sticky=W + E + N + S, pady=5, padx=5) self.listbox.grid(column=2, row=0, rowspan=4, columnspan=2, sticky=W + E + N + S, pady=5, padx=5) self.listbox.columnconfigure(0, weight=1) self.listbox.rowconfigure(0, weight=1) self.scrollbar.grid(column=1, sticky=N + S) # defining options for opening a directory self.dir_opt = options = {} options['initialdir'] = 'C:\\' options['mustexist'] = False options['parent'] = root options['title'] = 'This is a title' self.columnconfigure(0, weight=1) self.columnconfigure(1, weight=1) self.columnconfigure(2, weight=10) self.rowconfigure(0, weight=5) self.rowconfigure(1, weight=1) self.rowconfigure(2, weight=1) self.rowconfigure(3, weight=1) def combine_pictures(self): """Combines two pictures into one.""" items = self.listbox.curselection() items = [self.files[int(item)] for item in items] self.pbar["value"] = 0 self.pbar["maximum"] = int(round(len(items) / 2)) 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)) self.pbar["value"] += 1 root.update_idletasks() 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) 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) if __name__ == '__main__': root = Tk() root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) root.minsize(700, 500) root.title("ProjectLifer 1.0.1") ProjectLiferFrame(root).grid(row=0, column=0, sticky=(N, S, E, W)) root.mainloop()