Thumbnail creation

Creating Thumbnails from directory by using Tkinter file dialog and Pillow library

We will learn this script in two parts. First, we will directly assign variables to hold the path of source and destination directories.

In the second part we will integrate Tkinter file dialog to assign source and destination paths by the user for generation of thumbnail images.

Part I Creating Thumbnail images

By using all .png ( or .jpg ) files of the source directory we will create thumbnail images and store them in a destination directory.

Here we have assigned path and path2 variables with source and destination directories. If the destination directory is not created, then mkdir() can be used to create the same.

We used listdir() to create a list using all files and directories of the source path.
from PIL import Image
import os

path = "E:\\testing\\images"  # Source Path of Images
path2 = "E:\\testing\\images\\thumbnails"  # destination dir
#os.mkdir(path2) # create directory  
l1=os.listdir(path) # List of all files and directories at source
for file in l1:
    print(file) # Print file name
    if os.path.splitext(file)[1]=='.png':
        img ='\\'+file, mode="r")  # create  object  # display
        print(img.size) # main image size ( width, height)
        img.thumbnail((200, 200))  # max width, max height'\\'+file)  # save thumbnail  image
        #print(img.size)  # thumbnail size ( width, height)  # display thumbnail image

Part II : Integrating Tkinter filedialog to create Thumbnails

PIL Thumbnail source and destination directory selection
We will use Tkitner filedialog to browse and select one source directory.
The destination directory also will be set by the user by using filedialog askdirectory() method.

from PIL import Image
import os

import tkinter as tk
from tkinter import filedialog # to use file dialog

my_w = tk.Tk()
my_w.geometry("450x200")  # Size of the window width x height
my_w.title("")  #  title
path='' # string to hold source directory path 
path2='' # string to hold destination directory path
def my_fun1(): 
    global path 
    path = filedialog.askdirectory() # select directory 
    l1.config(text=path) # Show directory path in Label 

b1=tk.Button(my_w,text='Source directory',font=22,


def my_fun2(): 
    global path2 
    path2 = filedialog.askdirectory() # select directory 
    l2.config(text=path2) #  Show selected directory path

b2=tk.Button(my_w,text='Destination directory',font=22,

l2.grid(row=1,column=2,padx=2) #  show destination path


def create_thumbnails():
    global path,path2 
    if os.path.exists(path) and os.path.exists(path2):
        l1=os.listdir(path) # List of all files and directories
        for file in l1:
            #print(file) # Print file or directory names 
            if os.path.splitext(file)[1]=='.png': 
                img ='/'+file, mode="r")  # 
        # display
                print(img.size) # source image size ( width, height)
                img.thumbnail((200, 200))  # max width, max height
      '/'+file)  # save thumbnail  image
                #print(img.size)  # thumbnail size ( width, height)
        # display thumbnail image
my_w.mainloop()  # Keep the window open

Python Imaging Library PIL ImageDraw to add Line, rectangle, circle to an Image. ImageFilter
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer