PIL und Tkinter
Version vom 18. November 2004, 12:22 Uhr von Camhy (Diskussion | Beiträge)
Ein Beispiel einer Tkinter Oberfläche, die zwei Bilder anzeigt. Es werden ein paar (simple) Bildtransformationen demonstriert.
import sys from PIL import Image, ImageTk from Tkinter import Tk, Canvas, Button,Frame import Pmw import ImageEnhance root = Tk() root.title("Test") im = Image.open("desktop_07_m_1280x960.bmp").convert("RGB") im2= Image.open("desktop_07_m_1280x960.bmp").convert("RGB") imview=im imview2=im2 actualpercent_orig=1.0 actualpercent_modif=1.0 init=0 photo = ImageTk.PhotoImage(imview) photo2 = ImageTk.PhotoImage(imview2) pwmain = Pmw.PanedWidget(root,orient='vertical') panephoto=pwmain.add("top",min = .1, size = .95) panecontrol=pwmain.add("bottom",min = .05, size = .05) pwmain.pack(fill = 'both', expand = 1) pwphoto = Pmw.PanedWidget(panephoto,orient='horizontal') pane1=pwphoto.add("left",min = .1, size = .5) pane2=pwphoto.add("right",min = .1, size = .5) pwphoto.pack(fill = 'both', expand = 1) frameleft = Frame(pane1, background = 'grey90') frameleft.pack(fill = 'both', expand = 1) frameright = Frame(pane2, background = 'grey90') frameright.pack(fill = 'both', expand = 1) menuBarleft = Pmw.MenuBar(frameleft, hull_relief = 'raised', hull_borderwidth = 1) menuBarleft.pack(fill = 'x') menuBarleft.addmenu('View', '') menuBarright = Pmw.MenuBar(frameright, hull_relief = 'raised', hull_borderwidth = 1) menuBarright.pack(fill = 'x') menuBarright.addmenu('View', '') canvas=Pmw.ScrolledCanvas(frameleft,borderframe=1,labelpos='n', label_text="Original", hscrollmode='dynamic',vscrollmode='dynamic') image1=canvas.create_image(0, 0, image=photo, anchor="nw") canvas.pack(fill = 'both', expand = 1) canvas.resizescrollregion() canvas2=Pmw.ScrolledCanvas(frameright,borderframe=1,labelpos='n', label_text="Modified", hscrollmode='dynamic',vscrollmode='dynamic') image2=canvas2.create_image(0, 0, image=photo2, anchor="nw") canvas2.pack(fill = 'both', expand = 1) canvas2.resizescrollregion() def updateright(): global imview2,im2,photo2,canvas2,image2,actualpercent_modif,init imview2=im2.resize((int(im2.size[0]*actualpercent_modif), int(im2.size[1]*actualpercent_modif))) photo2 = ImageTk.PhotoImage(imview2) canvas2.delete(image2) image2=canvas2.create_image(0, 0, image=photo2, anchor="nw") canvas2.resizescrollregion() pass def updateleft(): global imview,im,photo,canvas,image1,actualpercent_orig imview =im.resize((int(im.size[0]*actualpercent_orig), int(im.size[1]*actualpercent_orig))) photo = ImageTk.PhotoImage(imview) canvas.delete(image1) image1=canvas.create_image(0, 0, image=photo, anchor="nw") canvas.resizescrollregion() pass class ViewClass: def __init__(self, which, percent): self.which = which self.percent=percent def __call__(self): global actualpercent_modif,actualpercent_orig,init if init==1: if self.which=='right': actualpercent_modif=self.percent/100. updateright() pass if self.which=='left': actualpercent_orig=self.percent/100. updateleft() pass pass pass pass def resize(): global im2 im2=im2.resize((500,500),Image.ANTIALIAS) updateright() def rotate(): global im2 im2=im2.rotate(45) updateright() for i in range(10): menuBarleft.addmenuitem('View', 'command', '', command = ViewClass('left',(i+1)*10), label = str((i+1)*10)+'%') menuBarright.addmenuitem('View', 'command', '', command = ViewClass('right',(i+1)*10), label = str((i+1)*10)+'%') init=1 Button(panecontrol, text="resize", command=resize).grid(row=0,column=0) Button(panecontrol, text="rotate", command=rotate).grid(row=0,column=1) Button(panecontrol, text="done", command=root.quit).grid(row=1,column=0) root.mainloop()