【发布时间】:2023-03-30 21:21:01
【问题描述】:
我有一个带有图像的tk.Label 对象(通过tk.Label(self.root,image='image.png')),并想为其添加一个滤色器,即使图像更红。我该怎么做?
【问题讨论】:
我有一个带有图像的tk.Label 对象(通过tk.Label(self.root,image='image.png')),并想为其添加一个滤色器,即使图像更红。我该怎么做?
【问题讨论】:
您可以使用 PIL 修改图像,并将其保存为新文件。在 Tkinter 中,您可以使用 PhotoImage(file="file.png") 将图像用作按钮:
# importing image object from PIL
from PIL import ImageOps, Image
import PIL.Image
# creating an image object
img = Image.open(r"test.png").convert("L")
# image colorize function
img = ImageOps.colorize(img, black="red", white="white")
# save the image as new.png
img.save("new.png")
from tkinter import *
root = Tk()
# set the img1 variable as the image we just created
img1 = PhotoImage(file="new.png")
b = Button(image=img1)
b.pack()
root.mainloop()
【讨论】:
ImageTk.PhotoImage()就可以了。