Resizing images with ImageTk.PhotoImage with Tkinter

TkinterPythonGUI-Programming

The PIL or Pillow library in Python is used for processing images in a Tkinter application. We can use Pillow to open the images, resize them and display in the window. To resize the image, we can use image_resize((width, height) **options) method. The resized image can later be processed and displayed through the label widget.

Example

Let us have a look at the example where we will open an image and resize it to display in the window through the label widget.

# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Load the image
image=Image.open('download.png')

# Resize the image in the given (width, height)
img=image.resize((450, 350))

# Conver the image in TkImage
my_img=ImageTk.PhotoImage(img)

# Display the image with label
label=Label(win, image=my_img)
label.pack()

win.mainloop()

Output

Running the above code will display a resized image in the window.

raja
Published on 06-Aug-2021 06:22:12
Advertisements