3

I have this virtual file called virtual-raster.vrt with the following gdalinfo of:

Band 1 Block=1x1 Type=Float32, ColorInterp=Gray

I just want to convert it into a palleted one like so:

Band 1 Block=1x1 Type=Byte, ColorInterp=Palette

With the following color palette (value:color):

0 : #000000
1 : #FF0000
2 : #00FF00
3 : #0000FF

How to convert a grayscale VRT to a palleted VRT using GDAL tools?

0

1 Answer 1

3

Read the documentation of VRT https://gdal.org/drivers/raster/vrt.html and add color table. Step by step example:

Create test image with gdal_create https://gdal.org/programs/gdal_create.html#gdal-create. Utility is included in GDAL version 3.2 and higher.

gdal_create -of GTiff -outsize 10 10 -bands 1 -burn 2 -ot Float32  float_gray.tif

Create a VRT from the test image with gdal_translate

gdal_translate -f vrt float_gray.tif float_gray.vrt

The result is close to your VRT, even I wonder the blocksize that you have, 1x1. Do you have only one pixel?

gdalinfo float_gray.vrt
Driver: VRT/Virtual Raster
Files: float_gray.vrt
       float_gray.tif
Size is 10, 10
Corner Coordinates:
...
Band 1 Block=10x10 Type=Float32, ColorInterp=Gray

Open the VRT file with text editor and add color table as in the documentation

ColorTable: This element is parent to a set of Entry elements defining the entries in a color table. Currently only RGBA color tables are supported with c1 being red, c2 being green, c3 being blue and c4 being alpha. The entries are ordered and will be assumed to start from color table entry 0.

<ColorTable>   
<Entry c1="0" c2="0" c3="0" c4="255"/>
<Entry c1="145" c2="78" c3="224" c4="255"/>
</ColorTable>

The whole VRT with reference to TIFF file and color table with three entries would look like

<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Float32" band="1">
    <ColorInterp>Gray</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">float_gray.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="10" RasterYSize="10" DataType="Float32" BlockXSize="10" BlockYSize="10" />
      <SrcRect xOff="0" yOff="0" xSize="10" ySize="10" />
      <DstRect xOff="0" yOff="0" xSize="10" ySize="10" />
    </SimpleSource>
    <ColorTable>
      <Entry c1="0" c2="0" c3="0" c4="255"/>
      <Entry c1="145" c2="78" c3="224" c4="255"/>
      <Entry c1="255" c2="78" c3="224" c4="255"/>
    </ColorTable>
  </VRTRasterBand>
</VRTDataset>

Gdalinfo about the modified VRT

gdalinfo float_gray.vrt
Driver: VRT/Virtual Raster
Files: float_gray.vrt
       float_gray.tif
Size is 10, 10
Corner Coordinates:
...
Band 1 Block=10x10 Type=Float32, ColorInterp=Palette
  Color Table (RGB with 3 entries)
    0: 0,0,0,255
    1: 145,78,224,255
    2: 255,78,224,255

I leave it to you to convert the colors in hexa format into RGB values.

2
  • Does this mean that there is no readily available GDAL tool for doing this? This answer of somewhat XML-parsing could be the only possible solution though. Oct 19, 2020 at 15:38
  • If all that you have is 0 : #000000 1 : #FF0000 2 : #00FF00 3 : #0000FF I doubt that you can find any full automatic way. Copying palette from an existing image might be possible but I do not know, just wondering.
    – user30184
    Oct 19, 2020 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.