From 21a23a0689c1582162ded0b913f495ea89c3be87 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 30 Nov 2023 16:17:04 +0100 Subject: [PATCH] pct2rgb.py: emit explicit exception when source file has no color table (fixes #8793) --- autotest/pyscripts/test_pct.py | 23 +++++++++++++++++++ swig/python/gdal-utils/osgeo_utils/pct2rgb.py | 2 ++ 2 files changed, 25 insertions(+) diff --git a/autotest/pyscripts/test_pct.py b/autotest/pyscripts/test_pct.py index d5c71f8b4de7..018bbf36f12a 100755 --- a/autotest/pyscripts/test_pct.py +++ b/autotest/pyscripts/test_pct.py @@ -170,6 +170,29 @@ def test_pct2rgb_1(script_path, tmp_path, rgb2pct1_tif): ori_ds = None +############################################################################### +# Test pct2rgb when invoked on a dataset without color table + + +def test_pct2rgb_no_color_table(script_path, tmp_path, rgb2pct1_tif): + gdal_array = pytest.importorskip("osgeo.gdal_array") + try: + gdal_array.BandRasterIONumPy + except AttributeError: + pytest.skip("osgeo.gdal_array.BandRasterIONumPy is unavailable") + + output_tif = str(tmp_path / "test_pct2rgb_no_color_table.tif") + + from osgeo_utils import pct2rgb + + with pytest.raises(Exception, match="has no color table"): + pct2rgb.pct2rgb( + src_filename="../gcore/data/byte.tif", + pct_filename=None, + dst_filename=output_tif, + ) + + ############################################################################### # Test rgb2pct -n option diff --git a/swig/python/gdal-utils/osgeo_utils/pct2rgb.py b/swig/python/gdal-utils/osgeo_utils/pct2rgb.py index 987acc6f8aa9..d7fe81538ac1 100644 --- a/swig/python/gdal-utils/osgeo_utils/pct2rgb.py +++ b/swig/python/gdal-utils/osgeo_utils/pct2rgb.py @@ -84,6 +84,8 @@ def pct2rgb( ct = get_color_table(pal) else: ct = src_band.GetRasterColorTable() + if ct is None: + raise Exception(f"{src_filename} has no color table") ct_size = ct.GetCount() lookup = [