I am selecting a file using a tkinter file dialog. I open the file, resize it, create a PhotoImage and then update my canvas. I end up getting no image.
Can someone explain how I can open an image file, resize it, and update my canvas with the resized image?
My code:
Can someone explain how I can open an image file, resize it, and update my canvas with the resized image?
My code:
from PIL import ImageTk, Image # To handle JPG files import tkinter as tk from tkinter import filedialog as fd new_image = None def change_image(): global new_image filetypes = (("PNG image", "*.png"), ("JPG image", "*.jpg"), ("All files", "*.*"),) file_picked = fd.askopenfilename(filetypes=filetypes) picked_image = Image.open(file_picked) resized_image = picked_image.resize((5, 5)) new_image = ImageTk.PhotoImage(resized_image) my_canvas.itemconfigure(canvas_image, image=new_image) # Start interface = tk.Tk() my_canvas = tk.Canvas(interface, width=700, height=700) my_image = tk.PhotoImage() open_button = tk.Button(interface, text="Open image", command=change_image) canvas_image = my_canvas.create_image((0, 0), anchor="nw", image=my_image) my_canvas.pack() open_button.pack() interface.mainloop()