ST_Grayscale
Prev | 12.12. Raster Processing: Map Algebra | Next

## Name

ST_Grayscale — Creates a new one-8BUI band raster from the source raster and specified bands representing Red, Green and Blue

## Synopsis

`(1) raster ST_Grayscale(`raster rast, integer redband=1, integer greenband=2, integer blueband=3, text extenttype=INTERSECTION`)`;

`(2) raster ST_Grayscale(`rastbandarg[] rastbandargset, text extenttype=INTERSECTION`)`;

## Description

Create a raster with one 8BUI band given three input bands (from one or more rasters). Any input band whose pixel type is not 8BUI will be reclassified using ST_Reclass.

This function is not like ST_ColorMap with the `grayscale` keyword as ST_ColorMap operates on only one band while this function expects three bands for RGB. This function applies the following equation for converting RGB to Grayscale: 0.2989 * RED + 0.5870 * GREEN + 0.1140 * BLUE

Availability: 2.5.0

## Examples: Variant 1

    SET postgis.gdal_enabled_drivers = 'ENABLE_ALL';
    SET postgis.enable_outdb_rasters = True;

    WITH apple AS (
        SELECT ST_AddBand(
            ST_MakeEmptyRaster(350, 246, 0, 0, 1, -1, 0, 0, 0),
            '/tmp/apple.png'::text,
            NULL::int[]
        ) AS rast
    )
    SELECT
        ST_AsPNG(rast) AS original_png,
        ST_AsPNG(ST_Grayscale(rast)) AS grayscale_png
    FROM apple;

original_png | grayscale_png

## Examples: Variant 2

    SET postgis.gdal_enabled_drivers = 'ENABLE_ALL';
    SET postgis.enable_outdb_rasters = True;

    WITH apple AS (
        SELECT ST_AddBand(
            ST_MakeEmptyRaster(350, 246, 0, 0, 1, -1, 0, 0, 0),
            '/tmp/apple.png'::text,
            NULL::int[]
        ) AS rast
    )
    SELECT
        ST_AsPNG(rast) AS original_png,
        ST_AsPNG(ST_Grayscale(
            ARRAY[
                ROW(rast, 1)::rastbandarg, -- red
                ROW(rast, 2)::rastbandarg, -- green
                ROW(rast, 3)::rastbandarg, -- blue
            ]::rastbandarg[]
        )) AS grayscale_png
    FROM apple;

## See Also

ST_AsPNG, ST_Reclass, ST_ColorMap

Prev | Up | Next
ST_ColorMap | Home | ST_Intersection