Open In App

Rename a folder of images using Tkinter

Last Updated : 30 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Python GUI – tkinter, os.listdir() method, os.rename() method

In this article, the task is to rename a batch of images using Python. Now say given n images in a folder having random names. For example, consider the images below.

Now the requirement is to rename them in ordered fashion and appending their original dimensions of respective image like image-0-362×326, image-1-351×414, …and so on. Doing this manually would be a tedious task but this target can be achieved using the rename() and listdir() methods in os module.

rename() method

In Python 3, rename() method is used to rename a file or directory. This method is a part of the os module and comes in extremely handy.

Syntax: os.rename(src, dst) 
Parameters:
src: It is the source address of the file to be renamed 
dst: It is the destination with the new name.

listdir() method

The listdir method lists out all the content of a given directory.

Syntax: list = os.listdir(‘Src’)
Parameters:
Src: It is the source to be listed out

The following code will do the job for us. Using the Tkinter module it will ask for the folder path in which all the images are stored. It traverses through the lists of all the images in xyz folder, defines the destination (dst), and source (src) addresses, and renames using rename() method.

Below is the implementation.

Python3




# Python 3 code to rename multiple image
# files in a directory or folder
 
 
import os 
from tkinter import messagebox
import cv2
from tkinter import filedialog
from tkinter import *
 
 
height1 = 0
width1 = 0
 
# Function to select folder to rename images
def get_folder_path():
     
    root = Tk()
    root.withdraw()
    folder_selected = filedialog.askdirectory()
     
    return folder_selected
 
 
# Function to rename multiple files
def submit():
     
    source = src_dir.get()
    src_dir.set("")
    global width1
    global height1
     
    input_folder = get_folder_path()
    i = 0
     
    for img_file in os.listdir(input_folder):
 
        file_name = os.path.splitext(img_file)[0]
        extension = os.path.splitext(img_file)[1]
 
        if extension == '.jpg':
            src = os.path.join(input_folder, img_file)
            img = cv2.imread(src)
            h, w, c = img.shape
            dst = source + '-' + str(i) + '-' + str(w) + "x" + str(h) + ".jpg"
            dst = os.path.join(input_folder, dst)
 
            # rename() function will rename all the files
            os.rename(src, dst)
            i += 1
             
    messagebox.showinfo("Done", "All files renamed successfully !!")
 
 
 
# Driver Code
if __name__ == '__main__':
    top = Tk()
    top.geometry("450x300")
    top.title("Image Files Renamer")
    top.configure(background ="Dark grey")
 
    # For Input Label
    input_path = Label(top,
                       text ="Enter Name to Rename files:",
                       bg ="Dark grey").place(x = 40, y = 60)
     
    # For Input Textbox
    src_dir = StringVar()
    input_path_entry_area = Entry(top,
                                  textvariable = src_dir,
                                  width = 50).place(x = 40, y = 100)
 
     # For submit button
    submit_button = Button(top,
                           text ="Submit",
                           command = submit).place(x = 200, y = 150)
 
    top.mainloop()


Output :

 



Previous Article
Next Article

Similar Reads

How to iterate through images in a folder Python?
In this article, we will learn how to iterate through images in a folder in Python. Method 1: Using os.listdirExample 1: Iterating through .png onlyAt first we imported the os module to interact with the operating system.Then we import listdir() function from os to get access to the folders given in quotes.Then with the help of os.listdir() functio
2 min read
Delete all the Png Images from a Folder in Python
Python is mostly used to automate tasks, including file management operations. Deleting all PNG images from a folder can be efficiently handled using Python. In this article, we will explore two different approaches to Deleting all the PNG images from a Folder in Python. Delete all the PNG images from a FolderBelow are the possible approaches to De
2 min read
Convert Images to PDFs using Tkinter - Python
Prerequisite: Tkinter, img2pdf Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article
3 min read
Loading Images in Tkinter using PIL
In this article, we will learn how 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 requirements - Use this command to install Tkinter : pip install python-tkUse this command to install PIL : pip i
3 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Reading Images With Python - Tkinter
There are numerous tools for designing GUI (Graphical User Interface) in Python such as tkinter, wxPython, JPython, etc where Tkinter is the standard Python GUI library, it provides a simple and efficient way to create GUI applications in Python. Reading Images With Tkinter In order to do various operations and manipulations on images, we require P
2 min read
How to Use Bitmap images in Button in Tkinter?
Prerequisite: Python GUI – tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. A bitmap is an array of binary data representing the values of pixels in an image. A GIF i
2 min read
How to Dynamically Resize Background Images - Tkinter
You might surely be worried about how to resize your background image with the change in screen size. In this article, We are creating an advanced-level GUI application using the Tkinter module using Python in which you need to resize the app window various times and dynamically resize background images. Modules Required: Tkinter: As the GUI applic
6 min read
How To Use Images as Backgrounds in Tkinter?
Prerequisite: Python GUI – tkinter , Frame In this article, We are going to write a program use image in the background. In Tkinter, there is no in-built function for images, so that it can be used as a background image. It can be done with various methods: Method 1: Using photoimage methods. When it comes to GUI based application, images play a vi
3 min read