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.2" class ProjectLiferFrame(Frame): files = list() def __init__(self, root): ttk.Frame.__init__(self, root) # define buttonframe self.buttonframe = Frame(self) self.butdirchooser = Button(self.buttonframe, text='Verzeichnis wählen', command=self.askdirectory) self.dirtext = Label(self.buttonframe, wraplength=150, width=10,anchor=W, justify=LEFT) self.butselall = Button(self.buttonframe, text='Alle wählen', command=self.select_all) self.butselnone = Button(self.buttonframe, text='Keine wählen', command=self.select_none) self.butcombine = Button(self.buttonframe, text='Kombiniere Bilder', command=self.combine_pictures) # define listbox and scrollbar self.listbox = Listbox(self, selectmode=MULTIPLE) self.scrollbar = Scrollbar(self.listbox, orient=VERTICAL) # configure listbox and scrollbar self.listbox.config(yscrollcommand=self.scrollbar.set) self.scrollbar.config(command=self.listbox.yview) self.bottom_hbar = Separator(self,orient=HORIZONTAL) # define progressframe self.pframe = Frame(self) # define Progresstext self.ptext = Label(self.pframe, text='', width=10, anchor=W) # define ProgressBar self.pbar = Progressbar(self.pframe) # layout buttonframe self.buttonframe.grid(row=0, column=0, sticky=N, padx=5, pady=5) self.butdirchooser.grid(row=0, column=0,columnspan=2, sticky=N + W + E, pady=5) self.dirtext.grid(row=1, column=0, columnspan=2, sticky=W +E, pady=5) self.butselall.grid(row=2, column=0, sticky=N, pady=5) self.butselnone.grid(row=2, column=1, sticky=N, pady=5) self.butcombine.grid(row=3, columnspan=2, sticky=N + W + E, pady=5) # layout listbox and scrollbar self.listbox.columnconfigure(0, weight=1) self.listbox.rowconfigure(0, weight=1) self.listbox.grid(row=0, column=1, sticky=W + E + N + S, pady=5, padx=5) self.scrollbar.grid(column=1, sticky=N + S) # Horizontal Bar self.bottom_hbar.grid(row=1, columnspan=2, sticky=W + E) # layout progressbar and text self.pframe.columnconfigure(1,weight=1) self.pframe.grid(row=2, column=0, columnspan=2, sticky=W+E+N+S, pady=5, padx=5) self.ptext.grid(row=0, column=0, sticky=W) self.pbar.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(0, weight=0) self.columnconfigure(1, weight=1) self.rowconfigure(0, weight=1) #self.rowconfigure(1, weight=0) 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.pbar["value"] = current self.pbar["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.pbar["value"] = current self.ptext["text"] = '{}/{}'.format(current, max) root.update_idletasks() self.ptext["text"] = 'Done!' self.pbar["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.dirtext['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()