Loading Images in Tkinter using PIL


Python, an incredibly flexible programming language, has a variety of libraries that can handle diverse tasks. Tkinter emerges as Python's default package when it comes to creating a graphical user interface (GUI). Similar to this, the Python Imaging Library (PIL) is frequently used for image processing. In order to properly explain how to load images in Tkinter using PIL, this guide combines the two and includes real-world examples.

Introduction to Tkinter and PIL

Let's quickly explain Tkinter and PIL before moving on to the main subject.

The default GUI toolkit for Python is called Tkinter. It is easy to use and offers a strong object-oriented interface to the Tk GUI toolkit for developing desktop applications.

Contrarily, PIL, currently referred to as Pillow, is a free library for the Python programming language that includes support for opening, modifying, and saving a wide range of image file types.

Installing the Necessary Libraries

You need to have Tkinter and PIL installed on your machine in order to continue with this tutorial. Python comes with Tkinter already installed. Using pip, you may install PIL (Pillow):

Loading Images with PIL in Tkinter

You must do the following actions in order to display an image in a Tkinter window:

  • Bring in the required libraries.

  • Use PIL to display the image.

  • Create a photo image that is compatible with Tkinter using the image object.

  • To display the image, create a Tkinter Label or Canvas widget.

Let's look at a few instances.

Example 1: Basic Image Loading

This is a simple illustration of how to load a local image.

from tkinter import Tk, Label
from PIL import Image, ImageTk

# Initialize Tkinter window
root = Tk()

# Open image file
img = Image.open('path_to_image.jpg')
# Convert the image to Tkinter format
tk_img = ImageTk.PhotoImage(img)

# Create a label and add the image to it
label = Label(root, image=tk_img)
label.pack()

# Run the window's main loop
root.mainloop()

Example 2: Resizing the Image

Before displaying an image, you can scale it using PIL's resize() method if its size is too huge for your application.

from tkinter import Tk, Label
from PIL import Image, ImageTk

# Initialize Tkinter window
root = Tk()

# Open image file
img = Image.open('path_to_image.jpg')

# Resize the image
img = img.resize((200, 200), Image.ANTIALIAS)

# Convert the image to Tkinter format
tk_img = ImageTk.PhotoImage(img)

# Create a label and add the image to it
label = Label(root, image=tk_img)
label.pack()

# Run the window's main loop
root.mainloop()

Example 3: Loading Images from URLs

It may occasionally be necessary to load a picture from a URL. Along with PIL and Tkinter, you can utilise the urllib and io libraries for this.

import io
import urllib.request
from tkinter import Tk, Label
from PIL import Image, ImageTk

# Initialize Tkinter window
root = Tk()

# URL of the image
url = 'https://example.com/path_to_image.jpg'

# Open URL and load image
with urllib.request.urlopen(url) as u:
   raw_data = u.read()

# Open the image and convert it to ImageTk format
im = Image.open(io.BytesIO(raw_data))
img = ImageTk.PhotoImage(im)

# Create a label and add the image to it
label = Label(root, image=img)
label.pack()

# Run the window's main loop
root.mainloop()

Conclusion

This article has provided a comprehensive tutorial on loading images in Tkinter using PIL, a need when creating Python GUIs. Even though these are simple examples, they provide a solid framework on which you can construct applications that are more intricate.

Never forget that constant practise is the key to mastering any talent. Don't stop there, then. Use PIL to experiment with various image modifications and Tkinter to develop a variety of GUIs.

Updated on: 18-Jul-2023

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements