Tkinter – Display Image

Tkinter Label – Display image

In Tkinter, you can display images using the Label widget and the PhotoImage class.

Tkinter - Display Image
Tkinter – Display image

To display an image using a Label widget in Tkinter, create a PhotoImage object with the required image, and then pass this PhotoImage object as argument to Label class constructor via image parameter.

The code snippet would be as shown in the following.

label = tk.Label(window, image=photo)

Now, let us come to the image part photo, the second argument. This must be a PhotoImage object.

You can create a PhotoImage object using tkinter library as shown in the following, by directly providing the path of the image.

photo = tkinter.PhotoImage(file="background-1.png")

But, there are a very limited image formats that the tkinter.PhotoImage supports.

Therefore, it is recommended that you use Python Image Library PIL to create a PhotoImage object that supports a wide range of image formats.

from PIL import ImageTk, Image

image = Image.open("image.jpg")
photo = ImageTk.PhotoImage(image)

In this tutorial, you will learn how to display an image in a Label widget in Tkinter, with examples.

Example

Consider that we have an image file aurora.png next to our Python file main.py.

In this example, we display the image aurora.png in a label widget.

Program

import tkinter as tk
from PIL import ImageTk, Image

# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("500x300")

# Load the image
image = Image.open("aurora.png")
photo = ImageTk.PhotoImage(image)

# Label widget to display the image
label = tk.Label(window, image=photo)
label.pack()

# Run the application
window.mainloop()

Output

Run on a MacOS.

Tkinter - Display Image - Example
Tkinter – Display image – Example

Summary

In this Python Tkinter tutorial, we learned how to display an image in a Label widget, with examples.

Code copied to clipboard successfully 👍