Renaming Photos in Bulk with Python

Renaming Photos in Bulk with Python

Photos taken during a trip can take you back to some of your most cherished memories, but it’s an endless battle to keep them all organized, so that future you doesn’t have to sift through a formless blob of indecipherably numbered jpg files just to find the right one. Renaming photos individually is very time consuming - not to mention incredibly boring!

So, with the help of a short python script, you can rename whole swaths of photos at once per a specific and informative naming scheme at the push of a button. In this article, we will go through the process of writing such a script, should you want your own, but you can find the completed function here: https://github.com/AlexGordienko/photo-crawler, ready to use out of the box.

Getting Started

This script is written in Python 3, and the only module that you’ll need is the os module, which already comes standard with Python. You can install Python 3 here if you need to.  

The goal is to write a function that takes a directory of photos as input and renames them all with the following naming scheme: Location-Date-PhotographerInitials-Enumeration.jpg (ex. London-Jan2020-AG-1.jpg)

Building Your Re-Namer

Below is the completed code, you can have a look at it as an overview before we go through the major sections:

import os

# Function to rename multiple photos
def main():
    # ask user for some info about photos on run.
    print("Please follow the instructions below. You will be asked some "
          "identifying info about your photos. If you do not know or "
          "wish not to fill it out, just press ENTER without leaving a "
          "response. The only required question is the first one. Once"
          "you have filled out a response, press ENTER to submit it.")
    directory = str(input("Please enter the directory of your photos"))
    location = input("Please enter the location where these photos were taken.")
    date = input("Please enter the date when these photos were taken.")
    credit = input("What are the initials of the photographer?")

    i = 0
    for filename in os.listdir(directory):
        newName = directory + "/" + str(location) + "-" + str(date) + "-" + str(credit) + "-" + str(i) + ".jpg"
        oldName = directory + "/" + filename


        os.rename(oldName, newName)
        i += 1
    print("Done!")

if __name__ == '__main__':
    main()

We start by importing the os module, defining our main function, and showing the user some instructions on using the file.

import os

# Function to rename multiple photos
def main():
    print("YOUR INSTRUCTIONS HERE")

if __name__ == '__main__':
    main()

Next, we will prompt the user for input of the directory in which their images are stored (ex. Desktop/photos), the location where the photos were taken, the date that the photos were taken, and their initials. This gives us all the necessary information to put together an informative file name that is robust against dates and photographers.

directory = str(input("Please enter the directory of your photos"))
location = input("Please enter the location where these photos were taken.")
date = input("Please enter the date when these photos were taken.")
credit = input("What are the initials of the photographer?")

The next step is create a loop to run through all the files in the given directory with os.listdir(), where the the function parameter is a string to the relevant directory. Notice also that we create an iterator (i) to enumerate our photos to make sure that every photo is names differently.

The function that does the renaming is os.rename() which takes an input of the old file name and the new file name. For example:

os.rename("Desktop/photos/photo1.jpg", "Desktop/photos/London-Jan2020-AG-1.jpg")

Please note that the file names also contain the full path to that file.

i = 0
for filename in os.listdir(directory):
    newName = directory + "/" + str(location) + "-" + str(date) + "-" + str(credit) + "-" + str(i) + ".jpg"
    oldName = directory + "/" + filename

    os.rename(oldName, newName)
    i += 1
print("Done!")

Within the for loop, we put together the strings for the new image name and old image name and go through one by one renaming them all. After the for loop finishes we print out a helpful “Done!” as the process ends.

If you have been following along in your IDE of choice, you can simply run the script there, but if you want to be able to run it from the CLI at anytime, you can run the following command (for Mac):

python3 PATH/TO/SCRIPT/photo-crawler/main.py

There you have it! A short 25 lines of code to save you hours of time in the future. Feel free to add more tooling and case handling to this function to take care of situations were files aren't JPGs within the entered directory, or even attach a GUI to make this script extra user friendly. You can also change how the naming scheme works to better fit your organizational needs.

If you have any questions, please reach out on twitter @alexgordienko_ or open a GitHub Issue.

Show Comments