Using HSL and LCHuv to adjust hue on white image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mtcarlso
Posts: 7
Joined: 2014-07-17T11:43:40-07:00
Authentication code: 6789

Using HSL and LCHuv to adjust hue on white image

Post by mtcarlso »

I am trying to color the white (with black stitching) tie on this image [https://s3.amazonaws.com/mtcarlso_help/help/white.png] with some predetermined colors so that the stitching and light/shading details remain. I have met with some success but can't get quite close enough to the desired effect.

My best result has been to use this image [https://s3.amazonaws.com/mtcarlso_help/help/input.png] with a basic red hue as a starter, and to modulate the HSL for a subset of colors with this simple modulate command:

Code: Select all

convert input.png -mask mask.png -modulate [values] output.png
I have been testing with these colors to almost-acceptible results:
Royal: 22,200,30 [https://s3.amazonaws.com/mtcarlso_help/ ... _royal.png]
Poppy: 42,200,98 [https://s3.amazonaws.com/mtcarlso_help/ ... _poppy.png]
Plum: 10,200,60 [https://s3.amazonaws.com/mtcarlso_help/ ... l_plum.png]

As you can see from this crude reference [https://s3.amazonaws.com/mtcarlso_help/ ... erence.png], the output is pretty close to the desired colors, especially poppy and royal. However, we're losing some luminance(?) and a little bit of black detail. This is particularly evident in the plum sample. I have played around with the LCHuv and LCHab color spaces, also using modulate commands but have not been able to create desirable results. For example, this modification [https://s3.amazonaws.com/mtcarlso_help/ ... l_plum.png] seems to be the inverse of what I want dark red where it should be light and light red where it should be dark.

Any recommendations?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using HSL and LCHuv to adjust hue on white image

Post by fmw42 »

I do not know if you can use any arbitrary colorspace. One would have to look in the code. It does accept HSL, HSB and as of 6.8.4.7 LCHuv and LCHab. I think a better colorspace might be HSI or HCLp, but I do not know if those work or default to HSL, if not. I would have to test that or you could.

But to work with other colorspace, you need to use the -define modulate:colorspace , see

http://www.imagemagick.org/Usage/color_ ... colorspace
http://www.imagemagick.org/Usage/color_ ... dulate_HCL


You can work with any colorspace by converting the image to that colorspace and then modifying the Hue and or Sat/Chroma channels as desired, then converting back.
mtcarlso
Posts: 7
Joined: 2014-07-17T11:43:40-07:00
Authentication code: 6789

Re: Using HSL and LCHuv to adjust hue on white image

Post by mtcarlso »

Thank you for the quick reply. HCLp and HSI look identical to HSL. Any other thoughts?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using HSL and LCHuv to adjust hue on white image

Post by fmw42 »

mtcarlso wrote:Thank you for the quick reply. HCLp and HSI look identical to HSL. Any other thoughts?

Do the processing as I suggested by converting to HCLp (HSI), separate channels, then combine.

see

http://www.imagemagick.org/Usage/color_basics/#separate
http://www.imagemagick.org/Usage/color_ ... bine_other

Can you post your exact command with values for modulate and your mask image for the image you posted?

I can then try to provide code. Are you on Window or Linux/Mac? What version of IM. I would do this on my Mac, so unix syntax for IM commands.
mtcarlso
Posts: 7
Joined: 2014-07-17T11:43:40-07:00
Authentication code: 6789

Re: Using HSL and LCHuv to adjust hue on white image

Post by mtcarlso »

Ahh, I am sorry, I wasn't catching you fully. I will try that now. Thank you for the deep links.

I am running the script through a few different colors, but here are 3 that I have been testing with:

Code: Select all

convert input.png -mask mask.png -modulate 22,200,30 royal.png
convert input.png -mask mask.png -modulate 42,200,98 poppy.png
convert input.png -mask mask.png -modulate 10,200,60 plum.png
The mask I am using is here: https://s3.amazonaws.com/mtcarlso_help/help/mask.png

I am on a Mac running Version: ImageMagick 6.8.8-6 Q16
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using HSL and LCHuv to adjust hue on white image

Post by fmw42 »

This command does not seem to work for me on Mac OSX with IM 6.8.9.5 Q16.

Code: Select all

convert input.png -mask mask.png -modulate 10,200,60 plum.png

The result is a black tie. Are you sure you have the arguments to -modulate in the correct order?
mtcarlso
Posts: 7
Joined: 2014-07-17T11:43:40-07:00
Authentication code: 6789

Re: Using HSL and LCHuv to adjust hue on white image

Post by mtcarlso »

It certainly makes sense to me to start from the white (https://s3.amazonaws.com/mtcarlso_help/help/white.png) and apply the values from there, I had just had success starting from input.png and so was working from there.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using HSL and LCHuv to adjust hue on white image

Post by fmw42 »

mtcarlso wrote:It certainly makes sense to me to start from the white (https://s3.amazonaws.com/mtcarlso_help/help/white.png) and apply the values from there, I had just had success starting from input.png and so was working from there.
You cannot modulate to change saturation black/gray/white. They have no saturation to change.

Also see my edited comments above. Your command does not work for me. I get a black tie as a result starting with input.png (with the reddish tie).

Also red hue is the same as black hue, both have hue=0.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using HSL and LCHuv to adjust hue on white image

Post by fmw42 »

try something like this

Code: Select all

color="plum"
convert input.png \
\( -clone 0 -colorspace gray +level-colors black,plum \) \
\( mask.png -negate \) -compose over -composite input_$color.png
You can change the color to anything you want (rgb, hex, colornames, hsl etc)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using HSL and LCHuv to adjust hue on white image

Post by snibgo »

The modulate command won't work well on white.png. In that image, the tie is grey, which means the saturation is very low, and the hue is all over the place. When you shift the hue and increase the saturation, the hue will still be all over the place.

I wouldn't use "-modulate". Instead, something like this seems more obvious. The required colour is given in "rgb(...)". Windows BAT syntax:

Code: Select all

%IM%convert ^
  white.png ^
  ( +clone ^
    -level 0%%,70%% ^
    -alpha set -channel A -evaluate set 20%% +channel ^
    ( +clone -fill rgb(100%%,50%%,50%%) -colorize 100 -alpha off ) ^
    +swap -composite ^
  ) ^
  ( mask.png -negate ) ^
  -composite ^
  r.png
snibgo's IM pages: im.snibgo.com
mtcarlso
Posts: 7
Joined: 2014-07-17T11:43:40-07:00
Authentication code: 6789

Re: Using HSL and LCHuv to adjust hue on white image

Post by mtcarlso »

@fmw42 - Strange that you were getting a black tie. I reran and still have the same result. The redish tie with the command netted me a dark purple tie. That aside, your other solution works quite well. It does seem that the result loses just a very small bit of brightness or luminance or white (I am not sure which). For example, with royal (#F9FF4B) the result is just a touch darker than I would expect (you can see the desired result in the big blue swatch at the top-center of https://s3.amazonaws.com/mtcarlso_help/ ... erence.png) - any thoughts or tweaks?

@snibgo - Will try to check out your solve before bed. Thanks very much for responding.

And my thanks to both of you for the clarifications on the modulation of (effectively) hue-less colors.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using HSL and LCHuv to adjust hue on white image

Post by fmw42 »

If you want the color to be a bit brighter change black to grayX (gray5). Adjust X as desired.


color="#F9FF4B"
convert input.png \
\( -clone 0 -colorspace gray +level-colors black,$color \) \
\( mask.png -negate \) -compose over -composite input_$color.png

color="#F9FF4B"
convert input.png \
\( -clone 0 -colorspace gray +level-colors gray5,$color \) \
\( mask.png -negate \) -compose over -composite input2_$color.png
Post Reply