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?
-
Does this answer your question?: stackoverflow.com/a/1186847/1446289. Or possibly this: stackoverflow.com/a/3781869/1446289Aaron– Aaron ♦2020年03月10日 22:08:44 +00:00Commented Mar 10, 2020 at 22:08
2 Answers 2
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')
-
1Thanks, 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'])Nathan Raley– Nathan Raley2020年03月11日 15:45:45 +00:00Commented 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!Nathan Raley– Nathan Raley2020年03月19日 15:47:05 +00:00Commented 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??Nora 27– Nora 272021年04月04日 14:33:35 +00:00Commented Apr 4, 2021 at 14:33
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]
.