# declarations
import glob
import argparse
import os
import shutil
import sys
rootPath = os.getcwd()
# set up command line argument parser
parser = argparse.ArgumentParser(prog=’duplicate’)
parser.add_argument(‘-s’, ‘–source’, required=True)
parser.add_argument(‘-d’, ‘–dest’, required=True)
parser.add_argument(‘-m’, ‘–multisuffix’, type=str, default=”x2″)
parser.add_argument(‘-p’, ‘–pixelsize’, type=float, default=0.15)
# get arguments
argList = sys.argv[1:] # drop the script name parameter
args = parser.parse_args(argList)
dests = args.dest.split(” “) # multiple dests supported
source = args.source
multiSuffix = args.multisuffix
pixelSize = args.pixelsize
# search for source
filessourcePath = os.path.normpath(rootPath + “/” + source + “*.jpg”)
for file in glob.glob(sourcePath):
fileName = os.path.basename(file)
fileNameParts = os.path.splitext(fileName)
fileNameBase = fileNameParts[0]
fileNameExt = fileNameParts[1]
fileNameSects = fileNameBase.split(“-“)
# loop through each possible destination
for destName in dests:
fileNameNew = destName + fileNameSects[1] + multiSuffix + “-” + fileNameSects[2] + multiSuffix
FNNJpg = fileNameNew + “.jpg”
FilePathNew = os.path.normpath(rootPath + “/” + FNNJpg)
if os.path.isfile(FilePathNew):
# File copying / writing loop
print fileName + ” ^^ ” + FNNJpg
# read world file
worldFileName = fileNameBase + “.jgw”
worldFilePath = os.path.normpath(rootPath + “/” + worldFileName)
worldFile = open(worldFilePath, “r”)
worldFileLines = worldFile.readlines()
worldFile.close()
WFNNew = fileNameNew + “.jgw”
WFPNew = os.path.normpath(rootPath + “/” + WFNNew)
print worldFileName + ” -> ” + WFNNew
# write new world file
worldFile = open(WFPNew, “w+”)
worldFile.write(str(pixelSize) + “n”)
worldFile.write(worldFileLines[1] + “n”)
worldFile.write(worldFileLines[2] + “n”)
worldFile.write(“-” + str(pixelSize) + “n”)
worldFile.write(worldFileLines[4] + “n”)
worldFile.write(worldFileLines[5] + “n”)
worldFile.close()
# copy xml files
auxFileName = fileNameBase + “.jpg.aux.xml”
auxFilePath = os.path.normpath(rootPath + “/” + auxFileName)
AFNNew = fileNameNew + “.jpg.aux.xml”
AFPNew = os.path.normpath(rootPath + “/” + AFNNew)
print auxFileName + ” -> ” + AFNNew
shutil.copyfile(auxFilePath,AFPNew)
auxFileName = fileNameBase + “.xml”
auxFilePath = os.path.normpath(rootPath + “/” + auxFileName)
AFNNew = fileNameNew + “.xml”
AFPNew = os.path.normpath(rootPath + “/” + AFNNew)
print auxFileName + ” -> ” + AFNNew
shutil.copyfile(auxFilePath,AFPNew)
The script is concluded by a simple block of code to directly copy the xml sidecars without modifying them in any way. It writes a status message for each of the two files.
The script has been tested for real world situations and is performing a stellar job.