5

Has anyone been able to successfully call gdal_pansharpen.py from your own Python script?

I have successfully used rasterio to combine my R, G, and B bands, but would like to perform a pansharpen on the final image using the gdal_pansharpen command.

How did you manage to make the call?

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Mar 10, 2020 at 21:49
1

2 Answers 2

6

For older versions of GDAL:

Ensure gdal_pansharpen.py is in your python path. You may need to add it by setting the PYTHONPATH env var before running your script, or within your script, i.e. sys.path.append('gdal_pansharpen/directory').

from gdal_pansharpen import gdal_pansharpen 
gdal_pansharpen(['', '-b', '1', '-b', '2', '-b', '3', 'input_pan.tif', 'input_multi.tif', 'output_pansharpened.tif'])

For GDAL == 3.2

from osgeo.utils import gdal_pansharpen
gdal_pansharpen(
 pan_name='input_pan.tif', 
 spectral_names=['input_multi.tif'], 
 band_nums=[1, 2, 3],
 dst_filename='output_pansharpened.tif')

For GDAL >= 3.3:

from osgeo_utils import gdal_pansharpen
gdal_pansharpen(
 pan_name='input_pan.tif', 
 spectral_names=['input_multi.tif'], 
 band_nums=[1, 2, 3],
 dst_filename='output_pansharpened.tif')
answered Mar 10, 2020 at 22:42
3
  • 1
    Thanks, I think that gets me pretty close. How would I specify the arguments with the parens to equate to this: gdal_pansharpen.py LC80420362016085LGN00_B8.TIF LC80420362016085LGN00_B4.TIF LC80420362016085LGN00_B3.TIF LC80420362016085LGN00_B2.TIF carrizo-20160325-oli-pan.tif -r bilinear -co COMPRESS=DEFLATE -co PHOTOMETRIC=RGB assuming I have the images stored as local variables b8, b4, b3, b2, and tif? Would it be equal to this? gdal_pansharpen([b8, b4, b3, b2, tif, '-r', 'bilinear', '-co', 'COMPRESS=DEFLATE', '-co', 'PHOTOMETRIC=RGB']) Commented Mar 11, 2020 at 15:45
  • Yea, turns out I also needed to specify the -nodata flag as well, otherwise the image was messed up due to the nodata messing with the overall statistics of the image. Unfortunately, the multi band format pansharpen call has a bug where you can't specify nodata, but the vrt route then calling it works just fine. Thanks! Commented Mar 19, 2020 at 15:47
  • i use this solution but there is an other error : ERROR 4: 1: No such file or directory. why?? Commented Apr 4, 2021 at 14:33
2
ERROR 4: 3: No such file or directory

I think code should be:

from osgeo.scripts.gdal_pansharpen import gdal_pansharpen
gdal_pansharpen(['', '-b', '1', '-b', '2', '-b', '3', 'input_pan.tif', 'input_multi.tif', 'output_pansharpened.tif'])

'' is argv[0].

Lilium
1,0651 gold badge8 silver badges17 bronze badges
answered Mar 29, 2021 at 6:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.