Rename a folder of images using Tkinter
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.
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)
src: It is the source address of the file to be renamed
dst: It is the destination with the new name.
The listdir method lists out all the content of a given directory.
Syntax: list = os.listdir(‘Src’)
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.