rgb2pct.py

将24位RGB图像转换为8位调色板。

简介

rgb2pct.py [-n colors | -pct palette_file] [-of format] <source_file> <dest_file>

描述

该实用程序将计算一个给定的RGB图像的最佳伪彩色表使用一个中值削减算法对一个下采样的RGB直方图。然后使用颜色表将图像转换为伪彩色图像。此转换利用Floyd Steinberg抖动(错误扩散)最大化输出图像的视觉质量。

-n <color>

选择生成的颜色表中的颜色数。默认为256。必须介于2和256之间。

-pct <palette_file>

Extract the color table from <palette_file> instead of computing it. Can be used to have a consistent color table for multiple files. The <palette_file> must be either a raster file in a GDAL supported format with a palette or a color file in a supported format (txt, qml, qlr).

-of <format>

选择输出格式。从GDAL 2.3开始,如果未指定,则从扩展名猜测格式(以前是GTiff)。使用短格式名称。只应使用支持伪彩色表的输出格式。

<source_file>

输入的RGB文件。

<dest_file>

将创建的输出伪彩色文件。

注意:rgb2pct.py是一个Python脚本,只有在GDAL是使用Python支持构建的情况下才能工作。

例子

如果需要手工创建调色板,最简单的文本格式可能是GDAL VRT格式。在下面的示例中,在一个文本编辑器中创建了一个VRT,它有一个4色的小调色板,其中RGBA颜色为238/238/238/255、237/237/237/255、236/236/236/255和229/229/229/255。

rgb2pct.py -pct palette.vrt rgb.tif pseudo-colored.tif
more < palette.vrt
<VRTDataset rasterXSize="226" rasterYSize="271">
    <VRTRasterBand dataType="Byte" band="1">
        <ColorInterp>Palette</ColorInterp>
        <ColorTable>
        <Entry c1="238" c2="238" c3="238" c4="255"/>
        <Entry c1="237" c2="237" c3="237" c4="255"/>
        <Entry c1="236" c2="236" c3="236" c4="255"/>
        <Entry c1="229" c2="229" c3="229" c4="255"/>
        </ColorTable>
    </VRTRasterBand>
</VRTDataset>