How can I use ImageMagick to replace 1.png's HSL lightness channel with 2.png's HSL lightness channel? I almost cannot understand the convert
syntax.
There are at least two ways to do it...
First, and slowest is using -fx
and the command will look like this:
convert 1.png 2.png -colorspace HSL -channel L -fx "v" -colorspace RGB result.png
That will load the two images, 1.png and 2.png, and convert them both to HSL colorspace. It will then produce a new Lighness channel by using the value from image v
(which is how ImageMagick refers to the second image in a sequence - the first being u
). It then converts the resultant image back to RGB colorspace.
The second method looks like this and will be faster, especially on larger images:
convert 1.png 2.png \
-colorspace HSL \
-separate \
-delete 2-4 \
-combine \
-set colorspace HSL \
-colorspace rgb \
output.png
Basically, it will load both images, convert both to HSL, and separate into layers. Layer 0 will be 1.png's Hue, layer 2 will be 1.png's Saturation and Layer 3 will be 1.png's Lightness. Layers 3-5 will be 2.png's Hue, Saturation and Lightness. Then we delete delete 1.png's Lightness and 2.png's Hue & Saturation then combine the three channels into a new image, convert it back to RGB and save it as result.png
To test it, I create 2 dummy input images like this:
convert -size 256x256 \
xc:red xc:lime +append \
\( xc:blue xc:black +append \) \
-append 1.png
and this
convert -size 512x512 gradient:black-white 2.png
then I apply my first technique, namely
convert 1.png 2.png -colorspace HSL -channel L -fx "v" -colorspace RGB result.png
and I get this
The second technique gives identical results.