diff --git a/scripts/gdal/gdal_bands.py b/scripts/gdal/gdal_bands.py index 36fdcd047..c5ddbdb86 100644 --- a/scripts/gdal/gdal_bands.py +++ b/scripts/gdal/gdal_bands.py @@ -55,6 +55,12 @@ def get_gdal_band_offset(file: str, info: Optional[GdalInfo] = None) -> List[str get_log().warn( "gdal_info_bands_failed", band_red=band_red is None, band_green=band_green is None, band_blue=band_blue is None ) + + # Not enough bands for RGB assume it is grey scale + if len(bands) < 3: + return ["-b", "1", "-b", "1", "-b", "1"] + alpha_band_info + + # Could be RGB assume it is RGB return ["-b", "1", "-b", "2", "-b", "3"] + alpha_band_info return ["-b", str(band_red["band"]), "-b", str(band_green["band"]), "-b", str(band_blue["band"])] + alpha_band_info diff --git a/scripts/gdal/tests/gdal_bands_test.py b/scripts/gdal/tests/gdal_bands_test.py index 5160285d5..1130f7714 100644 --- a/scripts/gdal/tests/gdal_bands_test.py +++ b/scripts/gdal/tests/gdal_bands_test.py @@ -44,9 +44,20 @@ def test_gdal_rgb_bands_detection() -> None: assert " ".join(bands) == "-b 1 -b 2 -b 3" +def test_gdal_default_grey_scale() -> None: + gdalinfo = fake_gdal_info() + add_band(gdalinfo, color_interpretation="Pallette") + + bands = get_gdal_band_offset("some_file.tiff", gdalinfo) + + assert " ".join(bands) == "-b 1 -b 1 -b 1" + + def test_gdal_default_rgb() -> None: gdalinfo = fake_gdal_info() - add_band(gdalinfo, color_interpretation="unknown") + add_band(gdalinfo, color_interpretation="R") + add_band(gdalinfo, color_interpretation="G") + add_band(gdalinfo, color_interpretation="B") bands = get_gdal_band_offset("some_file.tiff", gdalinfo)