Map Monochrome Image to Color Gradient with FFmpeg CLUT
This article explains how to map a monochrome (grayscale) image to a
custom color gradient using the clut (Color LookUp Table)
filter in FFmpeg. By utilizing a Hald CLUT image, you can easily convert
flat grayscale tones into vibrant duotone or multi-color gradient
images. You will learn how to generate an identity Hald CLUT, apply your
desired color gradient to it, and map it directly to your source image
using a single FFmpeg command.
Step 1: Generate an Identity Hald CLUT
To map colors accurately, you first need a neutral Hald CLUT image. This image acts as a matrix representing all possible RGB color values.
Run the following command to generate a level 8 Hald CLUT (which produces a 512x512 pixel image):
ffmpeg -f lavfi -i haldclutsrc=level=8 -frames:v 1 haldclut.pngStep 2: Apply Your Color Gradient to the CLUT
To map grayscale values to a color gradient, you must apply your
desired gradient to the generated haldclut.png image.
Because the identity CLUT contains a perfect diagonal of gray values
from black to white, colorizing this image will determine how those gray
values are mapped.
You can colorize the CLUT in two ways: 1. Using Image Editing
Software: Open haldclut.png in Photoshop, GIMP, or
Lightroom, apply a gradient map (e.g., mapping shadows to dark blue and
highlights to warm yellow), and save the image as
gradient_clut.png. 2. Using FFmpeg
Filters: You can apply FFmpeg filters like curves
or colorchannelmixer directly to the CLUT. For example, to
tint the CLUT with a blue-to-yellow duotone:
ffmpeg -i haldclut.png -vf "colorchannelmixer=rr=0.5:rg=0:rb=0.5:gr=0:gg=0.8:gb=0.2:br=0:bg=0.2:bb=0.9" gradient_clut.pngStep 3: Map the Monochrome Image to the Gradient CLUT
Once you have your gradient_clut.png, use the
clut filter to apply the color mapping to your monochrome
source image.
Run the following command:
ffmpeg -i input_monochrome.png -i gradient_clut.png -filter_complex "[0:v][1:v]clut" output_colorized.pngHow the Command Works:
-i input_monochrome.png: Specifies your source grayscale image as the first input (0:v).-i gradient_clut.png: Specifies your modified gradient Hald CLUT as the second input (1:v).-filter_complex "[0:v][1:v]clut": Sends both inputs to theclutfilter, which looks up the brightness values of the monochrome image and replaces them with the corresponding colored gradient values from the CLUT.output_colorized.png: The final color-mapped output image.