Tkinter和枕头操作

时间:2016-12-06 08:11:01

标签: python-3.x image-processing tkinter pillow tkinter-canvas

我需要一些帮助,

以下是在Tkinter中显示滑块的代码,

from Tkinter import *

def show_values():
    print (w1.get(), w2.get())

master = Tk()
w1 = Scale(master, from_=0, to=42)
w1.pack()
w2 = Scale(master, from_=0, to=200, orient=HORIZONTAL)
w2.pack()
Button(master, text='Show', command=show_values).pack()

mainloop()

以下是过滤器代码

from PIL import ImageFilter
im2 = im.filter(ImageFilter.MinFilter(3))

我想动态显示图像过滤,这样当我们滚动滑块并且图像得到更新时,传递给MinFilter()的参数应该会改变。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

Scalecommand=,它使用来自比例

的当前值执行函数
import Tkinter as tk
from PIL import Image, ImageTk, ImageFilter


def show_value_1(value):
    print('v1:', value)

    # filter image
    img = image.filter(ImageFilter.MinFilter(int(value)))

    # create new photo
    photo = ImageTk.PhotoImage(img)

    # update image in label
    l['image'] = photo

    # PhotoImage has to be assigned to global variable - problem with "garbage collector"
    l.photo = photo


def show_value_2(value):
    print('v2:', value)


master = tk.Tk()

image = Image.open("ball-1.png")
photo = ImageTk.PhotoImage(image)

l = tk.Label(master, image=photo)
l.pack()
l.photo = photo

w1 = tk.Scale(master, from_=1, to=42, command=show_value_1)
w1.pack()

w2 = tk.Scale(master, from_=1, to=200, orient=tk.HORIZONTAL, command=show_value_2)
w2.pack()

master.mainloop()

ball-1.png

enter image description here