在同一窗口中将图像效果应用于Python3的方法

时间:2019-04-15 21:27:51

标签: atom-editor python-3.7

所以事情是不久前我从python开始的,我正在尝试使一些代码起作用。基本上,我要创建一个包含图像的窗口,当按下某些按钮时,图像会发生变化(大小,模糊等)。我遇到的一些问题是仅模糊效果生效一次,并且调整大小效果会在较大/较小的图片上方创建另一个图片,而不会删除后者。有办法使所有这些工作吗?先感谢您。 我可能使用了太多的代码行,因为我仍然没有足够的知识,所以我完全意识到了它:)。

import tkinter
import cv2
import PIL.Image, PIL.ImageTk

def blur_image():
    print("Blur")
    global photo_blur
    global cv_img_blur

    cv_img_blur = cv2.blur(cv_img, (3, 3))
    photo_blur = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img_blur))
canvas.create_image(0, 0, image=photo_blur, anchor=tkinter.NW)

def reduce_image():
    print("Reduced")
    global photo_reduce
    global cv_img_reduce

cv_img_reduce = cv2.resize(cv_img, (0,0), fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
photo_reduce = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img_reduce))
canvas.create_image(0, 0, image=photo_reduce, anchor=tkinter.NW)

window = tkinter.Tk()
window.title("Project")

cv_img = cv2.cvtColor(cv2.imread("hqdefault.jpg"), cv2.COLOR_BGR2RGB)

height, width, no_channels = cv_img.shape

canvas = tkinter.Canvas(window, width = width, height = height)
canvas.pack()

photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img))

canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)

btn_blur=tkinter.Button(window, text="Blur", width=50, command=blur_image)
btn_blur.pack(anchor=tkinter.CENTER, expand=True)

btn_reduce=tkinter.Button(window, text="Reduce", width=50, command=reduce_image)
btn_reduce.pack(anchor=tkinter.CENTER, expand=True)

window.mainloop()

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

import tkinter
import cv2
import PIL.Image, PIL.ImageTk

blur_status = (3, 3)

def blur_image():

    print("Blur")
    global photo_blur, cv_img_blur, image, blur_status

    cv_img_blur = cv2.blur(cv_img, blur_status)
    photo_blur = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img_blur))
    canvas.delete(image)
    image = canvas.create_image(0, 0, image=photo_blur, anchor=tkinter.NW)

    n1, n2 = blur_status
    blur_status = (n1 + 1, n2 + 2)


def reduce_image():

    print("Reduced")
    global photo_reduce, cv_img_reduce, image

    cv_img_reduce = cv2.resize(cv_img, (0,0), fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
    photo_reduce = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img_reduce))
    canvas.delete(image)
    image = canvas.create_image(0, 0, image=photo_reduce, anchor=tkinter.NW)


window = tkinter.Tk()
window.title("Project")

cv_img = cv2.cvtColor(cv2.imread("home.png"), cv2.COLOR_BGR2RGB)

height, width, no_channels = cv_img.shape

canvas = tkinter.Canvas(window, width = width, height = height)
canvas.pack()

photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img))

image = canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)

btn_blur = tkinter.Button(window, text="Blur", width=50, command=blur_image)
btn_blur.pack(anchor=tkinter.CENTER, expand=True)

btn_reduce = tkinter.Button(window, text="Reduce", width=50, command=reduce_image)
btn_reduce.pack(anchor=tkinter.CENTER, expand=True)

window.mainloop()

在现实世界中,创造一个新的先例,创造一个新的世界,然后再想象一个古老的世界。每次调用函数时,La sfocatura sembrava funzionare独有的原始电压值(3,3),就称为“ lavoripiùvolte”,即valori。