In this article, we will learn hoe to load images from user system to Tkinter window using PIL module. This program will open a dialogue box to select the required file from any directory and display it in the tkinter window.
Install the requiredments –
Use this command to install Tkinter :
pip install python-tk
Use this command to install PIL :
pip install pillow
from tkinter import * # loading Python Imaging Library from PIL import ImageTk, Image # To get the dialog box to open when required from tkinter import filedialog |
Note: The ImageTk module contains support to create and modify Tkinter BitmapImage and PhotoImage objects from PIL images and filedialog is used for the dialog box to appear when you are opening file from anywhere in your system or saving your file in a particular position or place.
Function to create a Tkinder window consisting of a button –
# Create a windoe root = Tk() # Set Title as Image Loader root.title("Image Loader") # Set the resolution of window root.geometry("550x300 + 300 + 150") # Allow Window to be resizable root.resizable(width = True, height = True) # Create a button and place it into the window using grid layout btn = Button(root, text ='open image', command = open_img).grid( row = 1, columnspan = 4) root.mainloop() |
The Button object is created with text ‘open image’. On clicking it the open_image function will be invoked.
Function to place the image onto the window –
def open_img(): # Select the Imagename from a folder x = openfilename() # opens the image img = Image.open(x) # resize the image and apply a high-quality down sampling filter img = img.resize((250, 250), Image.ANTIALIAS) # PhotoImage class is used to add image to widgets, icons etc img = ImageTk.PhotoImage(img) # create a label panel = Label(root, image = img) # set the image as img panel.image = img panel.grid(row = 2) |
The openfilename function will return the file name of image.
Function to return the file name chosen from a dialog box –
def openfilename(): # open file dialog box to select image # The dialogue box has a title "Open" filename = filedialog.askopenfilename(title ='"pen') return filename |
To run this code, save it by the extension .py and then open cmd (command prompt) and move to the location of the file saved and then write the following –
python "filename".py
and press enter and it will run. Or can be run directly by simply double-clicking your .py extension file.
Output:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Apply changes to all the images in given folder - Using Python PIL
- How to compress images using Python and PIL?
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Rename a folder of images using Tkinter
- Reading Images With Python - Tkinter
- Loading Image using Mahotas - Python
- Loading Excel spreadsheet as pandas DataFrame
- Mahotas - Loading image as grey
- MoviePy – Loading Audio File
- MoviePy – Loading Audio File from Video
- PYGLET – Loading Media
- PYGLET – Loading Plain Text Document
- PYGLET – Loading Texture
- PYGLET – Loading a File
- PYGLET – Loading Attribute File
- PYGLET – Loading Animation
- PYGLET – Loading Program Resource
- PYGLET – Loading a Image
- PYGLET – Loading HTML Document
- PYGLET – Loading 3-D Model
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


