Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Loading Images in Tkinter using PIL

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:

1. Prerequisites:

First, install the Pillow library. You can do this using pip:

pip install Pillow

2. Loading and Displaying an Image:

Step 1: Import Necessary Libraries

import tkinter as tk
from PIL import Image, ImageTk

Step 2: Create the Main Application Window

root = tk.Tk()
root.title("Image Loading with PIL")

Step 3: Load and Display the Image

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)

Step 4: Run the Main Loop

root.mainloop()

3. Complete Code:

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()

Notes:

  • Remember to replace "path_to_image.jpg" with the path to your own image.
  • The loaded image will remain in memory as long as there's a reference to the photo object. It's a common mistake to forget this reference, leading to the image not being displayed.
  • You can use the various functionalities of 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.
  1. Python PIL ImageTk example:

    • Description: Demonstrate the usage of the ImageTk module from PIL to work with images in Tkinter.
    • Code Example:
      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()
      
  2. Tkinter PhotoImage from PIL Image:

    • Description: Convert a PIL Image object to a Tkinter PhotoImage for displaying in a Tkinter window.
    • Code Example:
      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()
      
  3. Loading and showing images in Tkinter canvas with PIL:

    • Description: Extend the previous example to display images on a Tkinter canvas using PIL.
    • Code Example:
      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()