Showing the legend for the custom color map

Finally, one interesting functionality is to provide the legend when showing your custom color map. This can be achieved with the color_map_custom_legend.py script.

In order to build the legend image, the build_lut_image() function performs this functionality. We first call the build_lut() function in order to get the lookup table. Then, we call np.repeat() in order to replicate this lookup table several times (this operation is repeated height times). Note that the shape of the lookup table is (256, 3). We want the shape of the output image to be height, 256, and 3, and so we can use np.repeat() together with np.newaxis(), like this:

image = np.repeat(lut[np.newaxis, ...], height, axis=0)

The output of this script can be seen in the following screenshot:

In the preceding screenshot, you can see the effect of applying two custom color maps to a grayscale image and showing the legend for each color map.