Python Forum
tkinter weird image displaying behaviour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter weird image displaying behaviour
#1
So I've been trying to learn to use the tkinter with python to display images, and everything worked fine, when I displayed image like this:

from tkinter import *
import tkinter as tk
from PIL import Image, ImageTk



window = Tk()
frame = tk.Frame(window, relief=RAISED, borderwidth=5, bg="yellow")
frame.grid()

image_path = '41Nc7sKMDgL.jpg'
original_image = Image.open(image_path)
original_image.thumbnail((400, 400) ,resample=Image.LANCZOS)
tk_image = ImageTk.PhotoImage(original_image)
image = tk.Label(frame, image=tk_image)
image.grid(column=0, row=0)

window.mainloop()
but then, when I try to refactor the code into a function for example, I doesn't work:

from tkinter import *
import tkinter as tk
from PIL import Image, ImageTk

def thumbnail(frm: tk.Frame, pth: str, sz: tuple[int, int] = (600, 600)):
  original_image = Image.open(pth)
  original_image.thumbnail(sz, resample=Image.LANCZOS)
  tk_image = ImageTk.PhotoImage(original_image)
  image = tk.Label(frm, image=tk_image)
  image.grid(column=0, row=0)

window = Tk()
frame = tk.Frame(window, relief=RAISED, borderwidth=5, bg="yellow")
frame.grid()

thumbnail(frame, '41Nc7sKMDgL.jpg')

window.mainloop()
In the first case, I get a window displaying an image as it should, but then in the last example, the window only shows a gray area where the image should be.

What am I doing wrong here?
Reply
#2
So I figured it out, turns out the references to the image get garbage collected the moment the interpreter leaves the function where I'm defining the image. All I had to do is keep the reference alive when I'm out of the function.

Here's what I did:

class thumbnail(tk.Label):
  def __init__(self, sz, frm, pth):
    self.original_image = Image.open(pth)
    self.original_image.thumbnail(sz, resample=Image.LANCZOS)
    self.tk_image = ImageTk.PhotoImage(self.original_image)
    tk.Label.__init__(self, frm, image=self.tk_image)
creating the thumbnail like this ensures that the references don't get garbage collected
Reply
#3
Class names should start with a capital letter.

Use **kwargs to allow passing additional keyword arguments to the base class. This makes your new class more flexible.
import tkinter as tk
from PIL import Image, ImageTk
 
 
class Thumbnail(tk.Label):
    def __init__(self, parent, filename, size=(32, 32), **kwargs):
        image = Image.open(filename)
        image.thumbnail(size, resample=Image.LANCZOS)
        self.image = ImageTk.PhotoImage(image)
        super().__init__(parent, image=self.image, **kwargs)


root = tk.Tk()
Thumbnail(root, "image.png").pack()
Thumbnail(root, "image.png", size=(64, 64)).pack()
Thumbnail(root, "image.png", text="With text", compound=tk.BOTTOM).pack()
Thumbnail(root, "image.png", borderwidth=5, relief=tk.SUNKEN).pack()

root.mainloop()
pyDream likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 5,967 Jul-28-2025, 09:40 AM
Last Post: enablespindle
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 13,907 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 8,771 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 3,862 Jul-22-2022, 10:26 AM
Last Post: menator01
  How to redraw an existing image to a different image TkInter zazas321 6 9,973 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 8,214 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 6,663 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 23,206 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  Latex image too big on tkinter using sympy 4096 1 4,436 Nov-05-2020, 08:05 AM
Last Post: DPaul
Photo Tkinter TEXT background image _ShevaKadu 5 12,440 Nov-02-2020, 10:34 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020