I have some tif images with their corresponding header which are in tfw format. as any other image header, it includes: the pixel size in both x and y direction, rotations and coordinates of the upper left corner. I would like to change the pixel size in these files which is the first and forth row. I did not know how to read tfw format. So first I changed the extension to txt and now trying to do the next step.
I have written the following script to read and replace the value in txt files. It works nicely for one file but it does not work for all the files in a folder. I would be thankful if someone could assist me.
First section is function to change the value in header and the second code part would apply over all the files.
#******************The file EditHeader2*********
def edit(infile, outfile):
for line in infile:
outfile.write(line.replace('0.2', '1.6'))
infile.close()
outfile.close()
return outfile
#****************** call the function************************
import os, os.path, EditHeader2
folder1= 'E:/.../edited headers'
folder2= 'E:/.../edited headers/1'
for filename in os.listdir(folder1):
infilename = os.path.join(folder1, filename)
if not os.path.isfile(infilename): continue
base, extension = os.path.splitext(filename)
if os.path.splitext(filename)[1] == '.txt':
infile = open(infilename, 'r')
outfile = open(os.path.join(folder2, '{}'.format(base, extension)), 'w')
EditHeader2.edit(infile, outfile)
something.txtthat are still "tif files in tfw format". (i.e. renaming the files really gains nothing).