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 = Image.open(path+'\\'+file, mode="r")  # create  object
        #img.show()  # display
        print(img.size) # main image size ( width, height)
        img.thumbnail((200, 200))  # max width, max height
        img.save(path2+'\\'+file)  # save thumbnail  image
        #print(img.size)  # thumbnail size ( width, height)
        #img.show()  # 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("www.plus2net.com")  #  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,
    command=lambda:my_fun1(),bg='lightgreen')
b1.grid(row=0,column=0,padx=10,pady=20)

l1=tk.Label(my_w,text='Source',bg='yellow',font=12)
l1.grid(row=1,column=0,padx=2)


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,
    command=lambda:my_fun2(),bg='lightgreen')
b2.grid(row=0,column=2,padx=10,pady=20)

l2=tk.Label(my_w,text='Destination',bg='yellow',font=12)
l2.grid(row=1,column=2,padx=2) #  show destination path

b3=tk.Button(my_w,text='GO',font=22,
    command=lambda:create_thumbnails(),bg='lightyellow')
b3.grid(row=0,column=3,padx=10,pady=20)

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 = Image.open(path+'/'+file, mode="r")  # 
                #img.show()  # display
                print(img.size) # source image size ( width, height)
                img.thumbnail((200, 200))  # max width, max height
                img.save(path2+'/'+file)  # save thumbnail  image
                #print(img.size)  # thumbnail size ( width, height)
                #img.show()  # 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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer