Tkinter relies on the Python Pillow (aka PIL) package for image processing capabilities. Using Pillow, a Tkinter function that displays a text-based message can instead display an image-based message.
Note that depending on the purpose of an image in a Tkinter application, different coding may be required. The reason for this is because images in applications can vary from background wallpaper, to positioned images that represent processes in underlying code, to clickable images or icons that perform an action when clicked.
How to Display Images with Tkinter’s Label Widget
Tkinter’s label widget can be used to display either images or text. To display an image requires the use of Image and ImageTk imported from the Python Pillow (aka PIL) package.
A label widget can display either PhotoImage or BitmapImage objects:
- The PhotoImage class is used to display grayscale or true color icons, as well as images in labels. Note that only GIF and PGM/PPM image formats are supported. For information on how you can work with more image formats, see the Pillow section below.
- The BitmapImage class is used to display only monochrome (two-color) images in labels.
Tkinter has three built-in Layout Managers that can be used to position labels containing images in a frame: pack, grid, and place. For example, a label can be placed in a frame using the place layout manager at x,y coordinates, as shown in the following example:
label1.place(x=160, y=60)
How to Use Pillow with Tkinter
Tkinter relies on Pillow for working with images. Pillow is a fork of the Python Imaging Library, and can be imported in a Python console as PIL. Pillow has the following characteristics:
- Support for a wide range of image file formats, including PNG, JPEG and GIF.
- Basic image processing and manipulation functionality
- Width and height options that can be used to set the Label size for an image. If a size is not specified, the Label will be just large enough to display its contents.
To check if Pillow is already installed, enter the following command in a Python console:
help('PIL')
If Pillow is not installed, you can install it with:
python3 -m pip install pillow
How to Manipulate Images with PIL and ImageTk
To import ImageTk and Image in a Python console, enter:
from PIL import ImageTk, Image
An image can be opened with the following code snippet:
image1 = Image.open("<path/image_name>")
The resize() option can be used to set an image’s height and width. In the following example, an image’s dimensions are set to 50 pixels high and wide:
image1 = img.resize((50, 50), Image.ANTIALIAS)
If an image size is not specified, the Label will be just large enough to display its contents.
How to Display a Background Image with Tkinter
The following example displays a background image in a frame:
Import tkinter from tkinter import * from PIL import Image, ImageTk root = Tk() # Create a photoimage object of the image in the path image1 = Image.open("<path/image_name>") test = ImageTk.PhotoImage(image1) label1 = tkinter.Label(image=test) label1.image = test # Position image label1.place(x=<x_coordinate>, y=<y_coordinate>) root.mainloop()
How to Display an Image on a Tkinter Button
The following example displays an image positioned on a button
import tkinter from tkinter import * from PIL import Image, ImageTk root = Tk() # Position text in frame Label(root, text = 'Position image on button', font =('<font_name>', <font_size>)).pack(side = TOP, padx = <x_coordinate#>, pady = <y_coordinate#>) # Create a photoimage object of the image in the path photo = PhotoImage(file = "</path/image_name>") # Resize image to fit on button photoimage = photo.subsample(1, 2) # Position image on button Button(root, image = photoimage,).pack(side = BOTTOM, pady = <y_coordinate#>) mainloop()
Next steps
Now that you know how to add images using Python’s Tkinter, let’s move on to other things you can do with Tkinter:
Python For Data Science
Pre-bundled with the most important packages Data Scientists need, ActivePython is pre-compiled so you and your team don’t have to waste time configuring the open source distribution. You can focus on what’s important–spending more time building algorithms and predictive models against your big data sources, and less time on system configuration.
Some Popular Python Packages for Data Science/Big Data/Machine LearningYou Get Pre-compiled – with ActivePython
- pandas (data analysis)
- NumPy (multi-dimensional arrays)
- SciPy (algorithms to use with numpy)
- HDF5 (store & manipulate data)
- Matplotlib (data visualization)
- Jupyter (research collaboration)
- PyTables (managing HDF5 datasets)
- HDFS (C/C++ wrapper for Hadoop)
- pymongo (MongoDB driver)
- SQLAlchemy (Python SQL Toolkit)
Frequently Asked Questions
Can I display an image in Python GUI?
It all depends on which GUI framework you’re using, but all of them offer the ability to work with images either directly, or in conjunction with Python’s most popular imaging library, pillow. Some ways you can add images to your Python user interface include:
- Adding image to labels
- Adding images to the background
- Adding images to buttons
For more information on working with GUI frameworks in Python, see Top 10 Python GUI Frameworks Compared.
How do you insert an image with Tkinter?
Tkinter images can be added to many of the widgets that Tk and Ttk support. However, Tkinter does not support images directly. Instead, to display an image requires the use of Image and ImageTk imported from the Python pillow package.
To add an image to a label widget at the following x,y coordinates:label1.place(x=160, y=60)
To place a predefined image called “test” in the background:label1 = tkinter.Label(image=test)
label1.image = test
label1.place(x=<x_coordinate>, y=<y_coordinate>)
To add an image to a button:photo = PhotoImage(file = "</path/image_name>")
Button(root, image = photoimage,).pack(side = BOTTOM, pady = <y_coordinate#>)
See the Tkinter Cheat Sheet for more tips on working with Tkinter.
Can Tkinter use JPG images?
Tkinter does not directly support image files like jpg. Instead working with Tkinter images requires the installation of Python’s popular imaging library, pillow. You can then add images to a number of Tkinter widgets, including:
- Images on Labels – supports only GIF and PGM/PPM image formats
- Background Images – support for a wide range of image file formats, including PNG, JPEG and GIF.
- Images on Buttons – supports a wide range of formats, including PNG, JPEG and GIF.
How do you add an image to a label in Python with Tkinter?
Tkinter has three built-in Layout Managers that can be used to position labels containing images in a frame: pack, grid, and place. For example, a label can be placed in a frame using the place layout manager at x,y coordinates, as shown in the following example:
label1.place(x=160, y=60)
Learn more about buttons, widgets, tables and other Tkinter how-to’s.