'Image not Rendering in Python, Tkinter, PIL [duplicate]
PIL Photo not Rendering
code :
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def resize_image(file_dir):
full_dir = f'{file_dir}'
unresized_pic = Image.open(full_dir)
resized = unresized_pic.resize((100, 300), Image.ANTIALIAS)
final_pic = ImageTk.PhotoImage(resized)
return final_pic
root.geometry('500x500')
# photo = PhotoImage(file='assests/forest01.png')
my_picture = Label(root, image=resize_image('assests/forest01.png'))
my_picture.pack()
root.mainloop()
The resized_image()
function should return a resized picture when called, however, it is not working. How can I fix it?
Solution 1:[1]
Use as below instead:
Edit: For reference as to why this is not working in the function, see here, as pointed out by @Cool Cloud. Photo "is a local variable which gets garbage collected", after leaving the function.
photo = resize_image('assests/forest01.png')
my_picture = Label(root, image=photo)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |