1.. _rgb2pct:
2
3================================================================================
4rgb2pct.py
5================================================================================
6
7.. only:: html
8
9    Convert a 24bit RGB image to 8bit paletted.
10
11.. Index:: rgb2pct
12
13Synopsis
14--------
15
16.. code-block::
17
18    rgb2pct.py [-n colors | -pct palette_file] [-of format] <source_file> <dest_file>
19
20Description
21-----------
22
23This utility will compute an optimal pseudo-color table for a given RGB image
24using a median cut algorithm on a downsampled RGB histogram.   Then it
25converts the image into a pseudo-colored image using the color table.
26This conversion utilizes Floyd-Steinberg dithering (error diffusion) to
27maximize output image visual quality.
28
29.. program:: rgb2pct
30
31.. option:: -n <color>
32
33    Select the number of colors in the generated
34    color table.  Defaults to 256.  Must be between 2 and 256.
35
36.. option:: -pct <palette_file>
37
38    Extract the color table from <palette_file> instead of computing it.
39    Can be used to have a consistent color table for multiple files.
40    The<palette_file> must be either a raster file in a GDAL supported format with a palette
41    or a color file in a supported format (txt, qml, qlr).
42
43.. option:: -of <format>
44
45    Select the output format. Starting with
46    GDAL 2.3, if not specified, the format is guessed from the extension (previously
47    was GTiff). Use the short format name. Only output formats
48    supporting pseudo-color tables should be used.
49
50.. option:: <source_file>
51
52    The input RGB file.
53
54.. option:: <dest_file>
55
56    The output pseudo-colored file that will be created.
57
58NOTE: rgb2pct.py is a Python script, and will only work if GDAL was built with Python support.
59
60Example
61-------
62
63If it is desired to hand create the palette, likely the simplest text format
64is the GDAL VRT format.  In the following example a VRT was created in a
65text editor with a small 4 color palette with the RGBA colors 238/238/238/255,
66237/237/237/255, 236/236/236/255 and 229/229/229/255.
67
68::
69
70    % rgb2pct.py -pct palette.vrt rgb.tif pseudo-colored.tif
71    % more < palette.vrt
72    <VRTDataset rasterXSize="226" rasterYSize="271">
73        <VRTRasterBand dataType="Byte" band="1">
74            <ColorInterp>Palette</ColorInterp>
75            <ColorTable>
76            <Entry c1="238" c2="238" c3="238" c4="255"/>
77            <Entry c1="237" c2="237" c3="237" c4="255"/>
78            <Entry c1="236" c2="236" c3="236" c4="255"/>
79            <Entry c1="229" c2="229" c3="229" c4="255"/>
80            </ColorTable>
81        </VRTRasterBand>
82    </VRTDataset>
83