簡體   English   中英

Python / Tkinter / PIL-縮放不起作用

[英]Python/Tkinter/PIL - Scale doesn't work

對我的簡單圖像查看器進行編碼后,我在其中擁有圖像並且可以按比例修改其亮度和對比度,因此遇到了我無法解決的Enhance類問題。 它不會給我任何錯誤,但是不能按我的意願工作。 我只想用秤移動它時調節亮度。 我還沒有實現第二個Enhance類,只是想制作第一個Enhance類並縮放圖像。 謝謝 :)

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()

看起來您正在嘗試從單個圖像文件創建兩個圖像對象。 您正在顯示一個圖像,但是正在更新另一個。 在您的Enhance類中,您將創建第二個self.tkim實例並將增強的圖像粘貼到該實例,但是該圖像不會在任何地方顯示。 正在顯示原始圖像不會被你的增強功能修改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM