I want to create a simple raster calculation using python and GDAL and specific gdal_calc.py in osgeo4w shell.

BUT any time that before finish my script python stopping I take error : python.exe stop working.

I have read to many post from users with this problem but without specific solution.

in my PATHEXT I missed '.PY' I try to add but without success.

here the first code :

gdal_calc =  'python C:/OSGeo4W64/bin/gdal_calc.py -A path/to/a.tif -B path/to/b.tif  --outfile=path/to/out.tif --calc="((4*A)+(10*B))+4.8"'

try to add pathext :

call("set PATHEXT = % PATHEXT %;.PY;.PYW")
gdal_calc =  'python C:/OSGeo4W64/bin/gdal_calc.py -A path/to/a.tif -B   path/to/b.tif  --outfile=path/to/out.tif --calc="((4*A)+(10*B))+4.8"'

in this case I get windows error 2

after I try this :

os.environ['PATHEXT'] = '.PY'
gdal_calc =  'python C:/OSGeo4W64/bin/gdal_calc.py -A path/to/a.tif -B   path/to/b.tif  --outfile=path/to/out.tif --calc="((4*A)+(10*B))+4.8"'

but in this case gdal_calc.py don't execute .

Any idea how to can work this script without error ?

  • What happens when you remove "python" from your call?
    – Jon
    Commented Mar 2, 2018 at 16:48
  • @Jon then just don't execute any other idea ? Commented Mar 2, 2018 at 19:05
  • When i use gdal_calc.py, I don't call python to open it. I also don't specify a path as it's already in my environment, so my call looks exactly like the examples here: gdal.org/gdal_calc.html I don't think I can help much because I'm not familiar with running from the osgeo4w shell, just command line.
    – Jon
    Commented Mar 2, 2018 at 19:12
  • @Jon because I try to run it in python script Commented Mar 3, 2018 at 15:17
  • Oh, in that case your script doesn't "work" because you're just assigning a string to gdal_calc, not actually calling anything. Try "subprocess.call(gdal_calc)" [you have to first import subprocess].
    – Jon
    Commented Mar 3, 2018 at 15:33

1 Answer 1


Here is a template I use when calling gdal tools from python:

import subprocess, sys
callstr = [sys.executable,
           '-A', 'path/to/a.tif',
           '-B', 'path/to/b.tif',
proc = subprocess.Popen(callstr, stdout=subprocess.PIPE,stderr=subprocess.PIPE)

Some things to note: When using subprocess, you shouldn't put quotes around your calc equation [I am not sure this is true; read it on another answer I can't find now]. The output variable stdout will contain any strings returned by the process, and stderr will return any errors. Because gdal_calc.py isn't an executable, you need to specify its path and include the sys.executable command, apparently.

Finally, if your rasters are relatively small, depending on your processing needs you might find it more useful to load them as numpy arrays and do your raster math with numpy. If this route sounds interesting, look at rasterio which makes reading/writing geotiffs pretty easy.

  • I try to use your code but I take error WindowsError(193, '\xd4\xef %1 \xe4\xe5\xed \xe5\xdf\xed\xe1\xe9 \xdd\xe3\xea\xf5\xf1\xe7 \xe5\xf6\xe1\xf1\xec\xef\xe3\xde Win32') in this line proc = subprocess.Popen(callstr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) Commented Mar 3, 2018 at 20:40
  • I know rasterio and numpy but I want now to learn GDAL now :) Commented Mar 3, 2018 at 20:46
  • @ChrisPapas Going backwards, huh? I started with GDAL/ogr/osr and now I appreciate the raster packages much more, although I still use a lot of functions I built with GDAL. Check my edit--apparently gdal_calc.py needs to be called differently. I could've sworn it had python bindings (hence the .py in its name) but I can't find anything about it.
    – Jon
    Commented Mar 3, 2018 at 21:39

Your Answer

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

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