2014-01-05 48 views
0

編碼我的簡單圖像查看器,我有一個圖像,並可以通過縮放來修改它的亮度和對比度,我遇到了無法解決的增強類問題。它不會給我任何錯誤,但它不起作用,因爲我想。當我移動它時,我只想調整亮度來調整亮度。我沒有實現第二個增強類,只是想在圖像上製作第一個增強類和縮放工作。謝謝:)Python/Tkinter/PIL - 縮放不起作用

import tkinter as tk 
from PIL import Image, ImageTk, ImageEnhance 

class ImageViewer(tk.Frame): 
    def __init__(self, master): 
     tk.Frame.__init__(self, master, background="green") 

     # for now, don't use images. 
     self.im = Image.open("plant.jpg") #choose your picture 
     self.tkim = ImageTk.PhotoImage(self.im) 

     # these three widgets make up our main layout 
     label = tk.Label(self, image=self.tkim, text="label") 
     e = Enhance(self,self.im, ImageEnhance.Brightness) 
     e1 = Enhance1(self, self.im) 

     label.pack(side="bottom", fill="both", expand=True) 
     e.pack(side="left", fill="both", expand=True) 
     e1.pack(side="right", fill="both", expand=True) 


class Enhance(tk.Frame): 
    def __init__(self, master,image, enhancer): 
     tk.Frame.__init__(self, master) 
     self.image = image 
     self.tkim = ImageTk.PhotoImage(image.mode, image.size) 
     self.enhancer = enhancer(image) 

     self.update_enhance("1.0") 
     s = tk.Scale(self, label="Brightness", orient=tk.VERTICAL,from_=3.0, to=-1.0, resolution=0.01,command=self.update_enhance) 
     s.set(self.value) 
     s.pack(side = "left", fill = "both", expand = True) 

    def update_enhance(self, value): 
     self.value = eval(value) 
     self.tkim.paste(self.enhancer.enhance(self.value)) 

    # width, height, and color are only temporary, they 
    # make it easy to see the frames before they have 
    # any content 

class Enhance1(tk.Frame): 
    def __init__(self, master, image): 
     self.image = image 

# width, height, and color are only temporary, they 
# make it easy to see the frames before they have 
# any content 
     tk.Frame.__init__(self, master, background="blue", width=100, height=100) 

if __name__ == "__main__": 
    root = tk.Tk() 
    ImageViewer(root).pack(fill="both", expand=True) 
    root.mainloop() 

回答

0

它看起來像你試圖從一個圖像文件創建兩個圖像對象。您正在顯示一張圖片,但正在更新另一張圖片在您的Enhance課程中,您將創建第二個self.tkim實例並將增強後的圖像粘貼到該實例,但該圖像未顯示在任何位置。顯示的的原始圖像未被您的增強功能修改。

+0

非常感謝,但我找不到解決它的方法。 – Chilcone

+0

@Chilcone:你的意思是「找不到解決辦法」?您是否嘗試過僅使用單個圖像,但它不起作用,或者您不知道如何只使用單個圖像? –

+0

我不知道如何只使用一個圖像。對不起,我的不特定的anwers。 – Chilcone