top of page

Image File Renaming

This challenge had the intended goal of being able to take a sequence of images, make a copy and rename that copy without disturbing the original. The "recipe" for this code is as follows.

 

1. Locate the directory for the files.

This is used so when I place the python script in the image folder, it will execute the script for files in the folder.

2. List off all .exr files.

This will select only the .exr files so that not everything in the folder is moved.

3. Create a destination folder for the copied files.

I added this step in order to better organize the new files. The only drawback is that if I want to change where they go, I'd have to manually go into the script and fix it.

4. Copy the .exr files into the new folder.

This part of the script moves the .exr files it finds into the previously determined destination folder.

5. Rename the copied files.

The script then takes the copied files and renames them.

 

I added an extra step to create a new directory for the renames and copied images to go into. That way the files are kept separate and more organized. One of the biggest challenges for this code was selecting only the .exr files and moving those. 

 

*Disclaimer - This is only an example using example images. Normally, these files will not start with 'first__' which narrows down being able to use this code on a broader basis.

# Renaming Files

 

import os

import os.path

import shutil

import inspect

import glob

 

# 1. Script must know where its living.

fullpath = inspect.getframeinfo(inspect.currentframe()).filename

dirpath = os.path.dirname(os.path.abspath(fullpath))

print dirpath

 

# 2. Lists all .exr files.

glob_pattern = os.path.join(dirpath, '*.exr')

paths = glob.glob(glob_pattern)

 

 

# 3. Creating the destination folder.

endpath = '/home/jhenni21/mount/stuhome/tech312/endimages'

try:

  os.makedirs(endpath);

except:

  print "Folder already exists."

 

# 4. Makes a copy of all files.

for path in paths:

fname = os.path.basename(path)

move = os.path.join(dirpath,fname)

if (os.path.isfile(move)):

 shutil.copy(move,endpath)

else:

 print 'failed'

 

# 5. Renames copied files in new folder.

if fname.startswith('first__'):

 srcpath = os.path.join(endpath, fname)

 dest = os.path.join(endpath, fname[7:])

 os.rename(srcpath, dest)

 print('processed file named %s' % dest)

else:

 print('ignoring %s' %fname)

In order to execute this script in linux, I need the python script as well as the executable script in the folder.

 

The executable script is as follows -

python rename.py

read

 

In windows, the executable script will be slightly different as it will be a .bat file.

 

The script is as follows -

C:\python27\python rename.py

pause

 

The other issue I had with this script was that it also created a copy that stayed in the original folder that also got renamed. I am unsure as to why it created 2 separate sets of copies, one in the original and one in the new folder.

bottom of page