How to add an image in Tkinter?

Images are very useful objects in any application. We can process Images in a Tkinter application using the Pillow or PIL package in Python. There are several built-in functions such as loading an image, extracting an image, configuring the image pane, etc.


In this example, we will add by asking the user to select an image from a dialog box and then, display it using the Label widget.

#Import the Tkinter library
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import Image, ImageTk
#Create an instance of Tkinter frame
win= Tk()
#Define the geometry
win.title("Image Gallery")
def select_file():
   path= filedialog.askopenfilename(title="Select an Image", filetype=(('image    files','*.jpg'),('all files','*.*')))
   label= Label(win, image= img)
   label.image= img
#Create a label and a Button to Open the dialog
Label(win, text="Click the Button below to select an Image", font=('Caveat 15 bold')).pack(pady=20)
button= ttk.Button(win, text="Select to Open", command= select_file)
button.pack(ipadx=5, pady=15)


Running the above code will display a window that contains a button to select the image file from the directory and display the image on the window.

Now, select any image from the local directory and display the output on the screen.