0

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?

Chris
  • 18,724
  • 6
  • 46
  • 80
Jaazim
  • 9
  • 3

1 Answers1

0

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)
Chris
  • 18,724
  • 6
  • 46
  • 80