[GIS] Merge tif files with python

gdalmergepython

I'm also having problems with running gdal_merge.py within another python script and I cannot run it. I already checked a similar problem here. My example is as follows:

First I set my workspace and locations:

sys.path.append('C:/Python27/ArcGIS10.2/Scripts/') 
import gdal_merge as gm workspace="D:/Satellitendaten/rapideye/img/testregion/cannyedge/out/"
os.chdir(workspace)

Then I run:

sys.argv = ['o', 'out.tif', 'allre1.tif', 'allre10.tif', 'allre11.tif', 'allre12.tif', 'allre13.tif', 'allre14.tif', 'allre15.tif', 'allre16.tif', 'allre17.tif', 'allre18.tif', 'allre2.tif', 'allre3.tif', 'allre4.tif', 'allre5.tif', 'allre6.tif', 'allre7.tif', 'allre8.tif', 'allre9.tif']

I get:

0…10…20…30…40…50…60…70…80…90…100 – done.

But also ERROR 4:

`out.tif' does not exist in the file system, and is not recognised as a supported dataset name.

So the output is not created. Maybe someone can help me out with the issue to merge different tif files to a single tif?

Best Answer

As crmackey notes in his comment, the first element in sys.argv needs to be the current script. You also need to use '-o' instead of 'o' as your first argument for gdal_merge

Try:

sys.path.append('C:/Python27/ArcGIS10.2/Scripts/') 
import gdal_merge as gm 
workspace="D:/Satellitendaten/rapideye/img/testregion/cannyedge/out/"
os.chdir(workspace)

sys.argv[1:] = ['-o', 'out.tif', 'allre1.tif', etc...]
Related Question