I am attempting to make a simple Tkinter app that displays an image on screen, and that reduces the brightness of the image when you click:
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk, ImageEnhance
def update_image(img):
enhancer = ImageEnhance.Brightness(img)
temp_img = enhancer.enhance(0.5)
temp_tkimg = ImageTk.PhotoImage(temp_img)
panel.configure(image=temp_tkimg)
root = Tk()
img = Image.open('img.png')
tkimg = ImageTk.PhotoImage(img)
panel = ttk.Label(root, image=tkimg)
panel.pack(side="bottom", fill="both", expand="yes")
panel.bind('<1>', lambda e: update_image(img))
root.mainloop()
This correctly displays the initial image, but after clicking, instead of rendering the new image it just disappears. I've tried saving the temp_tkimg
as a new png and reopening it the same way I do with the initial image but this doesn't work either:
def update_image(img):
enhancer = ImageEnhance.Brightness(img)
temp_img = enhancer.enhance(0.5)
temp_img.save('temp.png')
temp_tkimg = PhotoImage(file='temp.png')
panel.configure(image=temp_tkimg)
I feel like this is a pretty simple concept so not sure what I'm missing.
Cheers.