Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Loading images in Tkinter can be done directly for GIF and PGM/PPM images, but for other formats like PNG and JPEG, you will need external libraries. The Pillow
library, which is an improved fork of the original PIL
(Python Imaging Library), is widely used for this purpose.
Here's a step-by-step tutorial to load images in Tkinter using Pillow
:
First, install the Pillow
library. You can do this using pip
:
pip install Pillow
import tkinter as tk from PIL import Image, ImageTk
root = tk.Tk() root.title("Image Loading with PIL")
Using Pillow
, you can open the image and then convert it to a format (PhotoImage
or BitmapImage
) that Tkinter's Label
or Canvas
widget can use:
# Open an image file image = Image.open("path_to_image.jpg") # replace 'path_to_image.jpg' with your image's path. photo = ImageTk.PhotoImage(image) # Using the image in a label label = tk.Label(root, image=photo) label.pack(padx=20, pady=20)
root.mainloop()
import tkinter as tk from PIL import Image, ImageTk root = tk.Tk() root.title("Image Loading with PIL") # Open an image file image = Image.open("path_to_image.jpg") photo = ImageTk.PhotoImage(image) # Using the image in a label label = tk.Label(root, image=photo) label.pack(padx=20, pady=20) root.mainloop()
photo
object. It's a common mistake to forget this reference, leading to the image not being displayed.Pillow
to process the image, like cropping, resizing, rotating, and more, before displaying it in Tkinter.Pillow
supports a variety of image file formats, giving you a lot of flexibility compared to Tkinter's built-in image loading capabilities.Python PIL ImageTk example:
ImageTk
module from PIL to work with images in Tkinter.from tkinter import Tk, Label from PIL import Image, ImageTk # Load an image using PIL image_path = "path/to/image.jpg" pil_image = Image.open(image_path) # Convert the PIL image to a Tkinter-compatible PhotoImage tk_image = ImageTk.PhotoImage(pil_image) # Create a Tkinter window and display the image root = Tk() label = Label(root, image=tk_image) label.pack() root.mainloop()
Tkinter PhotoImage from PIL Image:
Image
object to a Tkinter PhotoImage
for displaying in a Tkinter window.from tkinter import Tk, Label, PhotoImage from PIL import Image, ImageTk # Load an image using PIL image_path = "path/to/image.jpg" pil_image = Image.open(image_path) # Convert the PIL image to a Tkinter-compatible PhotoImage tk_image = ImageTk.PhotoImage(pil_image) # Create a Tkinter window and display the image root = Tk() label = Label(root, image=tk_image) label.pack() root.mainloop()
Loading and showing images in Tkinter canvas with PIL:
from tkinter import Tk, Canvas from PIL import Image, ImageTk def show_image_on_canvas(): # Load an image using PIL image_path = "path/to/image.jpg" pil_image = Image.open(image_path) # Convert the PIL image to a Tkinter-compatible PhotoImage tk_image = ImageTk.PhotoImage(pil_image) # Display the image on the canvas canvas.create_image(0, 0, anchor="nw", image=tk_image) root = Tk() root.title("Tkinter Canvas Image Display with PIL") # Create a Tkinter canvas canvas = Canvas(root, width=400, height=300) canvas.pack() # Button to show the image on the canvas show_image_button = Button(root, text="Show Image", command=show_image_on_canvas) show_image_button.pack() root.mainloop()