Skip to main content
the latter version wasn't corcerned to to correct place of the pixel (there was a bias)
Source Link
# Written by MerseyViking (mersey dot viking at gmail dot com), 2011.
# Released into the public domain - May 8, 2011
# I accept no responsibility for any errors or loss of data, revenue, or life this script may cause. Use at your own risk.

import osgeo.gdal as gdal
import osgeo.osr as osr
import os
import glob
import sys

def generate_tfw(path, gen_prj):
    for infile in glob.glob(os.path.join(path, '*.tif')):
        src = gdal.Open(infile)
        xform = src.GetGeoTransform()

        if gen_prj == 'prj':
            src_srs = osr.SpatialReference()
            src_srs.ImportFromWkt(src.GetProjection())
            src_srs.MorphToESRI()
            src_wkt = src_srs.ExportToWkt()

            prj = open(os.path.splitext(infile)[0] + '.prj', 'wt')
            prj.write(src_wkt)
            prj.close()

        src = None
        edit1=xform[0]+xform[1]/2
        edit2=xform[3]+xform[5]/2

        tfw = open(os.path.splitext(infile)[0] + '.tfw', 'wt')
        tfw.write("%0.8f\n" % xform[1])
        tfw.write("%0.8f\n" % xform[2])
        tfw.write("%0.8f\n" % xform[4])
        tfw.write("%0.8f\n" % xform[5])
        tfw.write("%0.8f\n" % xform[0]edit1)
        tfw.write("%0.8f\n" % xform[3]edit2)
        tfw.close()

if __name__ == '__main__':
    generate_tfw(sys.argv[1], sys.argv[2])
# Written by MerseyViking (mersey dot viking at gmail dot com), 2011.
# Released into the public domain - May 8, 2011
# I accept no responsibility for any errors or loss of data, revenue, or life this script may cause. Use at your own risk.

import osgeo.gdal as gdal
import osgeo.osr as osr
import os
import glob
import sys

def generate_tfw(path, gen_prj):
    for infile in glob.glob(os.path.join(path, '*.tif')):
        src = gdal.Open(infile)
        xform = src.GetGeoTransform()

        if gen_prj == 'prj':
            src_srs = osr.SpatialReference()
            src_srs.ImportFromWkt(src.GetProjection())
            src_srs.MorphToESRI()
            src_wkt = src_srs.ExportToWkt()

            prj = open(os.path.splitext(infile)[0] + '.prj', 'wt')
            prj.write(src_wkt)
            prj.close()

        src = None

        tfw = open(os.path.splitext(infile)[0] + '.tfw', 'wt')
        tfw.write("%0.8f\n" % xform[1])
        tfw.write("%0.8f\n" % xform[2])
        tfw.write("%0.8f\n" % xform[4])
        tfw.write("%0.8f\n" % xform[5])
        tfw.write("%0.8f\n" % xform[0])
        tfw.write("%0.8f\n" % xform[3])
        tfw.close()

if __name__ == '__main__':
    generate_tfw(sys.argv[1], sys.argv[2])
# Written by MerseyViking (mersey dot viking at gmail dot com), 2011.
# Released into the public domain - May 8, 2011
# I accept no responsibility for any errors or loss of data, revenue, or life this script may cause. Use at your own risk.

import osgeo.gdal as gdal
import osgeo.osr as osr
import os
import glob
import sys

def generate_tfw(path, gen_prj):
    for infile in glob.glob(os.path.join(path, '*.tif')):
        src = gdal.Open(infile)
        xform = src.GetGeoTransform()

        if gen_prj == 'prj':
            src_srs = osr.SpatialReference()
            src_srs.ImportFromWkt(src.GetProjection())
            src_srs.MorphToESRI()
            src_wkt = src_srs.ExportToWkt()

            prj = open(os.path.splitext(infile)[0] + '.prj', 'wt')
            prj.write(src_wkt)
            prj.close()

        src = None
        edit1=xform[0]+xform[1]/2
        edit2=xform[3]+xform[5]/2

        tfw = open(os.path.splitext(infile)[0] + '.tfw', 'wt')
        tfw.write("%0.8f\n" % xform[1])
        tfw.write("%0.8f\n" % xform[2])
        tfw.write("%0.8f\n" % xform[4])
        tfw.write("%0.8f\n" % xform[5])
        tfw.write("%0.8f\n" % edit1)
        tfw.write("%0.8f\n" % edit2)
        tfw.close()

if __name__ == '__main__':
    generate_tfw(sys.argv[1], sys.argv[2])
Source Link
MerseyViking
  • 14.6k
  • 1
  • 43
  • 76

The slickest way to generate TFWs is to write a script in Python or Java using GDAL, which would be a handful of lines of code.

Creation of old-style (pre ArcGis 9) .prj files are not supported GDAL, only reading (see here). New-style (based on WKT) files are supported for creation, but it's not guaranteed they cover all cases. But either way, in a supreme case of displacement activity, I have written a Python script that does what you need. There's no error checking or anything, but it works for the directory of tiffs I had to hand, YMMV.

# Written by MerseyViking (mersey dot viking at gmail dot com), 2011.
# Released into the public domain - May 8, 2011
# I accept no responsibility for any errors or loss of data, revenue, or life this script may cause. Use at your own risk.

import osgeo.gdal as gdal
import osgeo.osr as osr
import os
import glob
import sys

def generate_tfw(path, gen_prj):
    for infile in glob.glob(os.path.join(path, '*.tif')):
        src = gdal.Open(infile)
        xform = src.GetGeoTransform()

        if gen_prj == 'prj':
            src_srs = osr.SpatialReference()
            src_srs.ImportFromWkt(src.GetProjection())
            src_srs.MorphToESRI()
            src_wkt = src_srs.ExportToWkt()

            prj = open(os.path.splitext(infile)[0] + '.prj', 'wt')
            prj.write(src_wkt)
            prj.close()

        src = None

        tfw = open(os.path.splitext(infile)[0] + '.tfw', 'wt')
        tfw.write("%0.8f\n" % xform[1])
        tfw.write("%0.8f\n" % xform[2])
        tfw.write("%0.8f\n" % xform[4])
        tfw.write("%0.8f\n" % xform[5])
        tfw.write("%0.8f\n" % xform[0])
        tfw.write("%0.8f\n" % xform[3])
        tfw.close()

if __name__ == '__main__':
    generate_tfw(sys.argv[1], sys.argv[2])

Call it from the command line like this:

python gen_tfw.py <path_to_tiff_directory> [prj]

The second parameter can be prj to generate WKT-style prj files, or anything else to just generate .TFWs.

If you can't use Python scripts for whatever reason, you can use:

gdal_translate -co "TFW=YES" in.tif out.tif

But that will copy the image data too, so you'll have to delete the original. And of course, it won't generate .prj files of either flavour. But presuming all your tiffs are in the same projection, you could just hand-craft a .prj file and duplicate it for all the source images.