Please help! How replicate the function of GIMP's "colorize"

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?".
buddhaflow

Please help! How replicate the function of GIMP's "colorize"

Post by buddhaflow »

Hi! I have a problem I have spent hours and hours with. Any advice would be greatly appreciated.

I have some icons that are in a primary color and white. I want to dynamically change the colored part, but NOT the white/light part.

I stumbled onto Fred's "huemap", and at first I was jumping with joy - it seemed perfect. But then I realized that it could only change the hue, it could not change the saturation or lumination of the mapped sections.

I tried using -modulate, and this was good, but this changed the whole image. So if the luminosity was lowered, the white would become grey or even back.

I also tried using IM's "colorize function", but this too colored the white sections, UNLIKE the behavior or GIMP's "colorize."

I have even spent quite some time trying to modify the "huemap" script, but I can't get it to work. I see that he makes a 1*360 hue gradient space, and I'm not good enough at either IM or bash scripting to translate the functionality of this script to a 3 dimensional HSL space.

Any advice would be greatly appreciated, my whole project is stuck on this one thing.

Thank you so much!
Sasha Shepherd
Jeju, Korea
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

see http://www.imagemagick.org/Usage/color/#color_mods

be careful which colorspace you use HSL or HSB as one will work better with white, see http://www.imagemagick.org/Usage/color/ ... colorspace
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Please help! How replicate the function of GIMP's "color

Post by anthony »

You are probably looking for something like IM's -tint option
http://www.imagemagick.org/Usage/color/#tint

that only tint's the midtone colors, not primary colors.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

If tint is what you need, then you could also look at my scripts, tintilize and tricolorize.
buddhaflow

Re: Please help! How replicate the function of GIMP's "color

Post by buddhaflow »

Fred, you are heroic. Your herculean efforts on these IM scripts is just phenomenal, thank you so, so much.

I'm not on my Linux box right now, but I will try those two scripts this evening when I get home. Tricolorize looks EXACTLY like what I need.

Anthony, I tried -tint, and it was very close - thank you! However, it still kept the shades of the original color, instead of smoothly recoloring it to another. So with a red icon adding blue would result in a kind of purple, if I turned the image black and white first the end result would be kind of dull, etc. I might fiddle with it a bit more, however.

I will let you know if I have luck when I get back home and can work locally again.

Thank you!
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Fred, your tricolorize script looks like it does exactly what I need. Only problem is I am on Windows.

Looking at your examples, it looks like I would want to use -m "hsl(120,25%,50%)". Can you please tell me what the standard IM command line command context would be to replicate that?

I am new to Image Magick, and the more I learn the more I love this tool. I am trying to replicate the Gimp Colorize feature.

Thank you,

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

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

One way would be to install Cygwin. Then you could run all my scripts. If you need a more detailed explanation, I will get back to you later.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

I took a look at GIMP colorize and for the moment I am not sure what they are doing. But my tricolorize is not the equivalent. I will get back later if I can figure out what GIMP is doing there.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

OK. This is as close as I can get with the time I have.

hue=0
sat=50
light=0
hue=`convert xc: -format "%[fx:100*$hue/360]" info:`
sat=`convert xc: -format "%[fx:2*$sat]" info:`
convert zelda3.png -set colorspace RGB -colorspace gray -set colorspace sRGB \
-fill "hsl($hue%,$sat%,50%)" -tint 40% -fill white -colorize $light% show:

However, the above is using unix commands to compute variables.That will not work in Windows. But there are equivalent commands. Note also that in windows, % must be escaped to %%. See http://www.imagemagick.org/Usage/windows/

Sorry I am not a windows/dos/bat user and know little about Windows programming of IM.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

fmw42 wrote:OK. This is as close as I can get with the time I have.

hue=0
sat=50
light=0
hue=`convert xc: -format "%[fx:100*$hue/360]" info:`
sat=`convert xc: -format "%[fx:2*$sat]" info:`
convert zelda3.png -set colorspace RGB -colorspace gray -set colorspace sRGB \
-fill "hsl($hue%,$sat%,50%)" -tint 40% -fill white -colorize $light% show:

However, the above is using unix commands to compute variables.That will not work in Windows. But there are equivalent commands. Note also that in windows, % must be escaped to %%. See http://www.imagemagick.org/Usage/windows/

Sorry I am not a windows/dos/bat user and know little about Windows programming of IM.
Thank you sir, this will require a little research on my part to figure out how to use that but I am very grateful for your reply. I did install CygWin so I will see if I can figure that part out. I am essentially looking to tweak the Hue, Saturation & Brightness to make an image shift from one color to another. Looking to go from a purple over to green.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

I managed to get it thanks to your help. Here is the command that got me what I needed:

convert test.png -set colorspace RBG -colorspace HSL -set colorspace sRGB -fill "hsl(140,50,60%)" -tint 40% -fill "#05f65a" new.png

Now all I need to do is put that into a VBScript to loop through my images and change them all. That part is super easy.

Thanks again for pointing me in the right direction.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

For anyone trying to follow in my footsteps, I managed to do this on the PC with the following command:

convert test.png -set colorspace RBG -colorspace HSL -set colorspace sRGB -fill "hsl(140,50,60%)" -tint 40% -fill "#05f65a" new.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

markdmac wrote: convert test.png -set colorspace RBG -colorspace HSL -set colorspace sRGB -fill "hsl(140,50,60%)" -tint 40% -fill "#05f65a" new.png
I believe that this command has a couple of problems, even under unix.

First -fill "#05f65a" is doing nothing as it is not followed by -colorize as I had indicated.

Second hsl(140,50,60%) may not be a valid IM color. At best it is a rather odd set of values. I am not aware that you can properly mix non-percent and percent values. At least not in more current versions of IM. Either they are all % or all non-percent. The latter means that hue will be in range 0-360 and sat and light will be in range 0-255. See http://www.imagemagick.org/script/color.php. I assume that IM will interpret your value as if hsl(140%,50%,60%), since it sees one % somewhere. If that is the case, the hue will probably then be wrapped to 40% so that your value is hsl(40%,50%,60%), since hue is cyclical.

Upon testing, these are all equivalent:

convert -size 100x100 xc:"hsl(140,50,60%)" show:
convert -size 100x100 xc:"hsl(140%,50%,60%)" show:
convert -size 100x100 xc:"hsl(40%,50%,60%)" show:

The reason I had hsl(0%,100%,50%) is that is pure red, which is what I tested in GIMP. In HSL colorspace (double hexcone model see http://dougkerr.net/pumpkin/articles/HSL_HSV.pdf or http://what-when-how.com/computer-graph ... cs-part-2/) full brightness is at 50%. (note you can test colors with http://www.imagemagick.org/contrib/color-converter.php) Then I tinted with that specified color. Then colorized with white so that as in GIMP, light=0 is pure black and light=100 is pure white. In hsl light=100 will be black. That is why I left it fixed at 50% and added the -colorize step.


PS If you are doing this in Windows (without Cygwin), then all % need to be escaped as %%
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Thanks Fred, I wasn't really sure on all of the elements, however I can confirm that what I posted did give me my desired results. I will have to play with it to try and minimize the command.

All I am doing here is tweaking some icons from an Android phone for a custom color themed mod.

I am flabbergasted at how extensive IM is. So many of the options go far beyond my understanding of image files and formats. I greatly appreciate your guidance to help me learn.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

Are you trying to use the same command for all your icons? If so, there is a better way. You can create a hald image in IM. Then open it in GIMP (or PS) and apply whatever colorization you have been using in GIMP. Then bring the modified hald image back to IM and apply it via -hald-clut to any or all icons you want. That way you get exactly the same result that you had in GIMP.

see
http://www.imagemagick.org/Usage/color_mods/#hald-clut

If you want 24-bit color, use hald:8

_______________________________


PS. Going back to the earlier command, this chart may help.

Image

So if you want a blue colorization with saturation and lightness expressed in GIMP values (hue 0 to 360, sat 0 to 100, light -100 to 100), then



hue=240
sat=50
light=0
hue=`convert xc: -format "%[fx:100*$hue/360]" info:`
sat=`convert xc: -format "%[fx:2*$sat]" info:`
test=`convert xc: -format "%[fx:$light<0?0:1]" info:`
light=`convert xc: -format "%[fx:abs($light)]" info:`
if [ $test -eq 0 ]; then
fillcolor="black"
else
fillcolor="white"
fi
convert zelda3.png -set colorspace RGB -colorspace gray -set colorspace sRGB \
-fill "hsl($hue%,100%,50%)" -tint 40% \
-modulate 100,$sat,100 \
-fill $fillcolor -colorize $light% show:

Note: I have improved the code to better match GIMP values, especially for saturation and lightness.
Last edited by fmw42 on 2012-10-31T20:28:42-07:00, edited 4 times in total.
Post Reply