1

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)
5
  • What exactly is the problem with multiple files? Are some of them not converted? Any errors? Commented Jun 10, 2013 at 13:28
  • Thank you for your edition. Well, when I apply the main code, I receive the error, invalid syntax. and it refres to the last line EditHeader2.edit(infile, outfile) Commented Jun 10, 2013 at 13:36
  • what do you think would be the problem? Commented Jun 10, 2013 at 13:37
  • There's a missing bracket at the end of the one-but-last line. I don't think it was me who removed it. Had I not indented the code, it would have been invalid syntax as well. Commented Jun 10, 2013 at 13:40
  • Changing the extension on a file does not magically reformat the file into some new format. You now have files named something.txt that are still "tif files in tfw format". (i.e. renaming the files really gains nothing). Commented Jun 10, 2013 at 15:32

1 Answer 1

1

I left the EditHeader2 module unchanged.

This code in the main file works for me, even with multiple files.

import os, os.path, EditHeader2

folder1 = 'editedheaders'
folder2 = 'editedheaders/1'

extensions = ('.txt', '.tfw', 'your extensions here ...')

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] in extensions:
        infile = open(infilename, 'r')
        outfile = open(os.path.join(folder2, '{}'.format(base, extension)), 'w')
        EditHeader2.edit(infile, outfile)

So I barely changed anything. Maybe there were still problems with the indentation (some lines had 3 spaces, some 4 ...).

Sign up to request clarification or add additional context in comments.

2 Comments

: Thank you very much for your kind help. I tried it one more time and it works now. Just there is one question, the new created files don't have 'txt' file extension though there recognized at txt file. how can I solve this issue?
Updated the answer to allow you to define an arbitrary number of file extensions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.