Skip to content
Advertisement

Add color filter to Tk.Label

I have a tk.Label object with an image (via tk.Label(self.root,image='image.png')) and want to add a color filter to it, i.e. make the image more red. How can I do that?

Advertisement

Answer

You can modify the image with PIL, and save it as a new file. In Tkinter, you can use PhotoImage(file="file.png") to use the image as a button:

# 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()
Advertisement