【问题标题】:Add color filter to Tk.Label将颜色过滤器添加到 Tk.Label
【发布时间】:2023-03-30 21:21:01
【问题描述】:

我有一个带有图像的tk.Label 对象(通过tk.Label(self.root,image='image.png')),并想为其添加一个滤色器,即使图像更红。我该怎么做?

【问题讨论】:

    标签: python image tkinter


    【解决方案1】:

    您可以使用 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()就可以了。
    • 谢谢!经过一些试验,我发现 img=ImageOps.colorize(img,black='black',white='salmon') 可以为我完成这项工作。出于好奇,由于我在文档中找不到答案,“test.png”前面的 r 表示图像以只读模式打开?或者它是什么?
    • 不,就是说“常规字符串”,所以它不会像 \n 中的新行那样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2012-01-22
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多