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-362x326, image-1-351x414, …and so on. Doing this manually would be a tedious task but this target can be achieved using the
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 extremely handy.
Syntax: os.rename(src, dst)
src: It is the source address of the file to be renamed
dst: It is thw 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.
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course