Here is the full script as it is for now. I expect that I will pursue the idea of adding an option to feed in an input file, and will look at it next time I need to use the script, so there may be an additional part added to this series then.
import argparse
import os
import shutil
#parse command line parameters
parser = argparse.ArgumentParser(prog=’segments’)
parser.add_argument(‘-b’, ‘–base’, required=True)
parser.add_argument(‘-r’, ‘–right’, required=True)
parser.add_argument(‘-d’, ‘–down’, required=True)
parser.add_argument(‘-c’, ‘–counter’, type=int, default=4)
parser.add_argument(‘-p’, ‘–pixelsize’, type=float, default=0.1)
args = parser.parse_args()
#save the parameters
baseName = args.base
rightName = args.right
downName = args.down
counter = args.counter
pixelSize = args.pixelsize
#Input file names
rootPath = “/home/patrick/Sources/Segments/”
baseFileName = rootPath + baseName + “.jgw”
rightFileName = rootPath + rightName + “.jgw”
downFileName = rootPath + downName + “.jgw”
#read input files
baseFile = open(baseFileName, “r”)
baseData = baseFile.readlines()
baseFile.close()
rightFile = open(rightFileName, “r”)
rightData = rightFile.readlines()
rightFile.close()
downFile = open(downFileName, “r”)
downData = downFile.readlines()
downFile.close()
#save input file data
baseX = float(baseData[4])
baseY = float(baseData[5])
baseSkewX = float(baseData[1])
baseSkewY = float(baseData[2])
rightX = float(rightData[4])
rightY = float(rightData[5])
downX = float(downData[4])
downY = float(downData[5])
#Main calculation and processing section
#Initialisation
for colNum in range(counter):
for rowNum in range(counter):
segmentX = (((rightX-baseX)/counter)*colNum)+baseX
segmentY = (((downY-baseY)/counter)*rowNum)+baseY
gridX = “x” + str(counter) + “.” + str(colNum + 1)
gridY = “x” + str(counter) + “.” + str(rowNum + 1)
#Generate segment tile filename
baseNameBase = baseName
baseNameSplit = baseNameBase.split(“-“)
baseColDescriptor = baseNameSplit[0]
baseRowDescriptor = baseNameSplit[1]
gridRowDescriptor = baseRowDescriptor + gridY
gridColDescriptor = baseColDescriptor + gridX
gridFileName = gridColDescriptor + “-” + gridRowDescriptor + “.jpg”
# Next line for debugging output only
#print(gridX + ” ” + gridY + ” : ” + str(segmentX) + ” , ” + str(segmentY))
#Look for segment tiles that match current grid position
rootFilesList = os.listdir(rootPath)
for rootFile in rootFilesList:
if rootFile.endswith(gridFileName):
#Generate world file name
rootNameBase = os.path.splitext(rootFile)[0]
segmentName = rootNameBase + “.jgw”
segmentFileName = rootPath + segmentName
print(rootFile + ” -> ” + segmentName)
# Write world file
segmentFile = open(segmentFileName, “w+”)
segmentFile.write(str(pixelSize) + “n”)
segmentFile.write(str(baseSkewX) + “n”)
segmentFile.write(str(baseSkewY) + “n”)
segmentFile.write(“-” + str(pixelSize) + “n”)
segmentFile.write(str(segmentX) + “n”)
segmentFile.write(str(segmentY) + “n”)
segmentFile.close()
# find xml files for base layer and copy to segment
auxNameSource = baseNameBase + “.jpg.aux.xml”
auxFileNameSource = rootPath + auxNameSource
auxNameDest = rootNameBase + “.jpg.aux.xml”
auxFileNameDest = rootPath + auxNameDest
if os.path.isfile(auxFileNameSource):
print(” ” + auxNameSource + ” -> ” + auxNameDest)
shutil.copyfile(auxFileNameSource,auxFileNameDest)
auxNameSource = baseNameBase + “.xml”
auxFileNameSource = rootPath + auxNameSource
auxNameDest = rootNameBase + “.xml”
auxFileNameDest = rootPath + auxNameDest
if os.path.isfile(auxFileNameSource):
print(” ” + auxNameSource + ” -> ” + auxNameDest)
shutil.copyfile(auxFileNameSource,auxFileNameDest)