How can I display an image using Pillow in Tkinter?

TkinterServer Side ProgrammingProgramming

Python provides Pillow Package (PIL) to process and load the images in the application. An image can be loaded using the inbuilt Image.open("image location") method. Further, we can use a Label widget to display the Image in the window.

Example

#Import tkinter library
from tkinter import *
from PIL import Image,ImageTk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("750x550")
#Load the image
img= Image.open("tutorialspoint.jpg")
#Convert To photoimage
tkimage= ImageTk.PhotoImage(img)
#Display the Image
label=Label(win,image=tkimage)
label.pack()
win.mainloop()

Output

Running the above code will display an image in the window.

Before executing the code, make sure you have the image in the same project directory, or provide the absolute path of the image location.

raja
Published on 15-Apr-2021 17:04:47
Advertisements