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.
- Apply changes to all the images in given folder - Using Python PIL
- Python | OCR on All the Images present in a Folder Simultaneously
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Loading Images in Tkinter using PIL
- Reading Images With Python - Tkinter
- Rename multiple files using Python
- Rename all file names in your directory using Python
- Python | Pandas Dataframe.rename()
- How to rename columns in Pandas DataFrame
- Python | Pandas TimedeltaIndex.rename
- Python | Pandas Series.rename()
- Python | os.rename() method
- Python MongoDB- rename()
- Rename specific column(s) in Pandas
- Python | Create video using multiple images using OpenCV
- Erosion and Dilation of images using OpenCV in python
- Get emotions of images using Microsoft emotion API in Python
- Addition and Blending of images using OpenCV in Python
- Draw geometric shapes on images using OpenCV
- Python | Denoising of colored images using opencv
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.