Resizing pictures in PIL in Tkinter

TkinterServer Side ProgrammingProgramming

Python provides Pillow or PIL package for image processing which is used to load, process, and customize the images in an application. It has many properties like Color of the image, Image Font, resizing the images, loading the image, etc.

In order to resize the Images in an application, we can use the resize(width, height) method. The method can be invoked after loading the image in the application. In order to open the image in the application, we have to import the package in the notebook as,

from PIL import Image, ImageTk

Example

In the following example, we have resized an image to "300x225".

#Import tkinter library
from tkinter import *
from PIL import Image, ImageTk
#Create an instance of tkinter frame
win= Tk()
#Set the Geometry
win.geometry("750x250")
#Open a New Image
image= Image.open("tutorialspoint.png")
#Resize Image using resize function
resized_image= image.resize((300,225), Image.ANTIALIAS)
#Convert the image into PhotoImage
img = ImageTk.PhotoImage(resized_image)
#Create a label for the image
Label(win,image= img).pack(pady=20)
win.mainloop()

Output

Running the above code will display a window that contain an image with the size of "300x225".

raja
Published on 16-Apr-2021 07:14:19
Advertisements