Skip to content

Commit 8769520

Browse files
authored
rgb2pct.py: add --creation-option (OSGeo#12032)
Fixes OSGeo#12031
1 parent 809ad85 commit 8769520

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

autotest/pyscripts/test_pct.py

+20
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ def test_rgb2pct_2(script_path, rgb2pct2_tif):
202202
ds = None
203203

204204

205+
###############################################################################
206+
# Test rgb2pct --creation-option option
207+
208+
209+
def test_rgb2pct_creation_option(script_path, tmp_path):
210+
211+
tif_fname = str(tmp_path / "test_rgb2pct_creation_option.tif")
212+
213+
test_py_scripts.run_py_script(
214+
script_path,
215+
"rgb2pct",
216+
"--creation-option COMPRESS=LZW "
217+
+ test_py_scripts.get_data_path("gcore")
218+
+ f"rgbsmall.tif {tif_fname}",
219+
)
220+
221+
ds = gdal.Open(tif_fname)
222+
assert ds.GetMetadataItem("COMPRESSION", "IMAGE_STRUCTURE") == "LZW"
223+
224+
205225
###############################################################################
206226
# Test rgb2pct -pct option
207227

doc/source/programs/rgb2pct.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Synopsis
1515

1616
.. code-block::
1717
18-
rgb2pct [--help] [--help-general]
18+
rgb2pct [--help] [--help-general] [--creation-option OPTION]
1919
[-n colors | -pct palette_file] [-of format] <source_file> <dest_file>
2020
2121
Description
@@ -40,6 +40,11 @@ maximize output image visual quality.
4040
Select the number of colors in the generated
4141
color table. Defaults to 256. Must be between 2 and 256.
4242

43+
.. option:: --creation-option OPTION
44+
45+
Optional creation parameters for the GeoTIFF driver,
46+
for example "COMPRESS=LZW". Can be specified multiple times.
47+
4348
.. option:: -pct <palette_file>
4449

4550
Extract the color table from <palette_file> instead of computing it.

swig/python/gdal-utils/osgeo_utils/rgb2pct.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def rgb2pct(
3636
dst_filename: Optional[PathLikeOrStr] = None,
3737
color_count: int = 256,
3838
driver_name: Optional[str] = None,
39+
creation_options: Optional[list] = None,
3940
):
4041
# Open source file
4142
src_ds = open_ds(src_filename)
@@ -81,8 +82,18 @@ def rgb2pct(
8182

8283
gtiff_driver = gdal.GetDriverByName("GTiff")
8384

85+
# Convert options to list
86+
if isinstance(creation_options, str):
87+
creation_options = creation_options.split()
88+
if not creation_options:
89+
creation_options = []
90+
8491
tif_ds = gtiff_driver.Create(
85-
tif_filename, src_ds.RasterXSize, src_ds.RasterYSize, 1
92+
tif_filename,
93+
src_ds.RasterXSize,
94+
src_ds.RasterYSize,
95+
1,
96+
options=creation_options,
8697
)
8798

8899
tif_ds.GetRasterBand(1).SetRasterColorTable(ct)
@@ -175,6 +186,15 @@ def get_parser(self, argv) -> GDALArgumentParser:
175186
"palette or a color file in a supported format (txt, qml, qlr).",
176187
)
177188

189+
parser.add_argument(
190+
"--creation-option",
191+
"--co",
192+
dest="creation_options",
193+
default=[],
194+
action="append",
195+
help="GeoTIFF creation options, e.g. COMPRESS=LZW",
196+
)
197+
178198
parser.add_argument("src_filename", type=str, help="The input RGB file.")
179199

180200
parser.add_argument(

0 commit comments

Comments
 (0)