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

时间:2014-01-05 18:56:12

标签: python tkinter

编码我的简单图像查看器,我有一个图像,并可以通过比例修改其亮度和对比度,我遇到了我无法解决的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()

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试从单个图像文件创建两个图像对象。您正在显示一个图像,但更新另一个图像。在Enhance课程中,您要创建第二个self.tkim实例,并将增强的图像粘贴到该实例,但该图像不会在任何地方显示。 正在显示的原始图像未被增强功能修改。