I would like to call gdal_merge via batch-file, so far this looks like this:

python "C:\Program Files\GDAL\gdal_merge.py" -ot Float32 -of GTiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 -o "H:\test.tif" "H:\raster1.tif" "H:\raster2.tif"

This works already when I manually put the file names like above. Is there a way to automatically get all .tif-files in the folder as arguments?

Original post:

I want to automate the gdal_merge of 8 raster files (.tif) that are in the same folder using a high compression profile. Preferably I would like to use either python or a batch file.

My first try using python is based on a online tutorial:

from osgeo import gdal
import os
import subprocess

in_folder = r'C:\d2'
filelist = os.listdir(in_folder)

cmd = 'gdal_merge.py -ot Float32 -of GTiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 -o C:/Users/HYWA163/Documents/test.tif'

subprocess.call(cmd.split() + filelist)

Here I get the error "OSError: [WinError 193] %1 is not a permitted Win32 application"

Does anybody have an idea where the error is coming from or an alternative way of automating this merge outside of QGIS?

  • Can you post the contents of your --optfile?
    – GBG
    Dec 7, 2021 at 16:39
  • It was automatically created from QGIS (I copied the gdal command from here), but doesn't exist anynmore. I took it out of the code but it doesn't change anything.
    – PDistl
    Dec 7, 2021 at 16:40
  • You should update your post to include the correct command line argument.
    – GBG
    Dec 7, 2021 at 16:46
  • 1
    Maybe this answer helps: gis.stackexchange.com/a/236754/21355
    – Thomas
    Dec 7, 2021 at 16:47
  • 1
    Check Stack Overflow: stackoverflow.com/questions/25651990/…
    – GBG
    Dec 7, 2021 at 16:49

1 Answer 1


I probably wouldn't do this if working with many large rasters, but you can build a file list string of all .tif files in a directory with Python and insert it into your command string. I'm sure there are many ways to do this but the following worked for me (with my own paths of course). I kept the command very simple for testing, but once you get it working you can add create options etc.

import os
import subprocess

gm_path = 'C:\\Program Files\\GDAL\\gdal_merge.py'
out_folder = 'H:\\'
out_file = os.path.join(out_folder, 'merged.tif')
outdir = os.scandir(out_folder)
file_list = [os.path.join(out_folder, f) for f in outdir]
file_string = ' '.join(file_list)

cmd = f'python {gm_path} -o {out_file} {file_string}'


Another option could be to build a virtual raster from your input files using gdalbuildvrt, then use gdal_translate to convert to a geotiff.

import os
import subprocess

out_folder = 'H:\\'
out_vrt = os.path.join(out_folder, 'mosaic.vrt')
out_tif = os.path.join(out_folder, 'merged.tif')
build_vrt = f'gdalbuildvrt {out_vrt} {out_folder}\\*.tif'
merge = f'gdal_translate -of GTiff {out_vrt} {out_tif}'

You can check the answers to this similar question for further reference.

Not the answer you're looking for? Browse other questions tagged or ask your own question.