Skip to content

Commit

Permalink
pct2rgb.py: emit explicit exception when source file has no color tab…
Browse files Browse the repository at this point in the history
…le (fixes #8793)
  • Loading branch information
rouault committed Nov 30, 2023
1 parent 0573c8a commit 21a23a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions autotest/pyscripts/test_pct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions swig/python/gdal-utils/osgeo_utils/pct2rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 21a23a0

Please sign in to comment.