Apply Color to Layer Mask

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
soresuperstan
Posts: 6
Joined: 2017-09-27T19:42:15-07:00
Authentication code: 1151
Location: Los Angeles

Apply Color to Layer Mask

Post by soresuperstan » 2017-09-28T17:49:59-07:00

Hi,

I am very new to imagemagick but I found it to be super helpful so thank you. I am running into an issue and I do not where I went wrong. I have checked the documentation, read thru countless forums. I may have read the answer without knowing that it applied to my question.

My objective is to change the color of a product and for this example, it is a shoe. I know that I have to first create the mask and I did this using the chroma key masking method as shown here http://www.imagemagick.org/Usage/photos. I had to change some of the fuzz values to get it pretty close.

I start with this first image Image
My actual code to create the mask looks like this:

Code: Select all

magick convert shoe.jpg -modulate 100,100,20  -colorspace HSL ^
          -channel Hue,Saturation -separate +channel ^
	  ( -clone 0 -background none -fuzz 30%% +transparent grey64 ) ^
	  ( -clone 1 -background none -fuzz 0.5%% -transparent white ) ^
	  -delete 0,1 -alpha extract -compose multiply -composite ^
	  shoe_mask1.png
I get a good enough mask to work with. It becomes

Image

I then follow the same guidelines in the chroma key and smooth out the mask.

The code is (with some minor changes from the guide)

Code: Select all

magick convert shoe_mask1.png -morphology Thicken ConvexHull shoe_write_mask.png
It produces this image Image

Still following the chroma-mask guide I then change the color.

The code is

Code: Select all

magick convert shoe.jpg  -mask shoe_write_mask.png -modulate 150,100,170 +mask shoe-2.jpg 
it produces Image Which looks okay for my needs. During the time spent working on this, I was able to clear out more of the boxy shadow.

The issues I have are as follows

1. How can I change the color properly? at this time I have to mess with the modulation levels to get different colors and I do not think it is the correct way. I would like to be able to color the masked area to any CMYK, RGB or Hex Value so I am limited to guessing -modulate

2. Am I doing this the right way or am I taking a really long road instead of taking a shorter one? My goal is to be able to recolor products and have them look as real/natural as possible.

On the last bit of code above,

I have tried changing between convert to composite (following the syntax) with no luck
I have tried -colorspace RGB -fill rgb(x,x,x) with no luck
I have tried -background and it changes the entire image.

I am using Windows 10 and mostly command line. IM Version 7.0.7-0 Q16 x64

Any help would be really appreciated.

User avatar
fmw42
Posts: 22109
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Apply Color to Layer Mask

Post by fmw42 » 2017-09-28T18:00:18-07:00

If you are willing to use Windows 10 unix, you could try my script, replacecolor, at my link below.

soresuperstan
Posts: 6
Joined: 2017-09-27T19:42:15-07:00
Authentication code: 1151
Location: Los Angeles

Re: Apply Color to Layer Mask

Post by soresuperstan » 2017-09-28T18:09:27-07:00

Thanks @fmw42

I have actually found and read replacecolor on your page, I forgot to mention it. That's what told me it had to be possible.

Unfortunately, I would not know how to run unix on my machine. I know there is cygwin out there but I have never used it and I am not familiar with anything *nix except for maybe lists and changing directories.

User avatar
fmw42
Posts: 22109
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Apply Color to Layer Mask

Post by fmw42 » 2017-09-28T18:48:08-07:00

Windows 10 comes with unix. So you need not install Cygwin. Unfortunately, I am not a Windows user. So I cannot help much in that regard.

soresuperstan
Posts: 6
Joined: 2017-09-27T19:42:15-07:00
Authentication code: 1151
Location: Los Angeles

Re: Apply Color to Layer Mask

Post by soresuperstan » 2017-09-28T19:06:36-07:00

@fmw42

I just learned about that now. Trying to see if I could get it to work.

Thanks!

User avatar
fmw42
Posts: 22109
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Apply Color to Layer Mask

Post by fmw42 » 2017-09-28T19:19:54-07:00

See http://www.howtogeek.com/249966/how-to- ... indows-10/

P.S. My script does not accept masks. It creates its own mask from the input color.

soresuperstan
Posts: 6
Joined: 2017-09-27T19:42:15-07:00
Authentication code: 1151
Location: Los Angeles

Re: Apply Color to Layer Mask

Post by soresuperstan » 2017-09-29T09:37:04-07:00

Going to give it a try today. Thanks.

I have another question. In practicality are the steps that I take considered overkill or is there an easier process that I may have missed?

snibgo
Posts: 9406
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Apply Color to Layer Mask

Post by snibgo » 2017-09-29T10:21:02-07:00

By default, "-modulate" works in HSL space. For most purposes, HCL is better because the definitions of lightness and chroma are closer to human perception.
snibgo's IM pages: im.snibgo.com

soresuperstan
Posts: 6
Joined: 2017-09-27T19:42:15-07:00
Authentication code: 1151
Location: Los Angeles

Re: Apply Color to Layer Mask

Post by soresuperstan » 2017-09-29T11:54:23-07:00

Thanks snibgo. I will try that too.

I will post my results as for both Fred's scripts and changing to HCL.

Thanks again!

snibgo
Posts: 9406
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Apply Color to Layer Mask

Post by snibgo » 2017-09-29T12:39:53-07:00

HCL and HSL can both give the same results, given suitable parameters to "-modulate". But HCL is easier to control. For example, shifting the hue from blue to yellow in HSL will visually lighten it, but in HCL the visual lightness stays roughly constant.

If you have a patch on the image of a certain colour, and you want to change it to another colour, you can have a script that:

1. scales both patches to one pixel each;
2. converts those to HCL
3. subtracts the first channel, the "red" channel.

This gives the required shift for the hue in "-modulate".
snibgo's IM pages: im.snibgo.com

soresuperstan
Posts: 6
Joined: 2017-09-27T19:42:15-07:00
Authentication code: 1151
Location: Los Angeles

Re: Apply Color to Layer Mask

Post by soresuperstan » 2017-10-01T19:47:41-07:00

Here are my results.

I downloaded windows 10 unix bash shell. I got it up and running but I was not able to call the script and when I finally did I did not know what the error messages meant (I am not familiar with linux at all) it took me some time but I ended up downloading a virtualbox and installing Ubuntu.

I then read thru the documentation and installed the latest version of ImageMagick. I ran into a few problems with the delegates and installing them. This was my first interaction with any *nix system ever so I had no frame of reference. I tried a lot of suggestions but this is the one that worked https://askubuntu.com/questions/745660/ ... l-problems.

After that, I tested the replacecolor script and it worked! so thank you for that.

I also tried snibgo's suggestion and I messed with -set colorspace to HCL and then -modulate and I got some different results but I was/am still clueless as to how to apply a color and it is most likely due to not understanding certain fundamentals.

I appreciate both of your help and I will continue looking at the documentation.

Thanks again!

User avatar
fmw42
Posts: 22109
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Apply Color to Layer Mask

Post by fmw42 » 2017-10-01T19:53:11-07:00

-modulate shifts colors from their current values. It does not set colors to a specific value. You need to work in Hue space and convert your colors from one Hue value to another. That is basically what I do in my scripts. See my script, huemap, also. It works by hue values and their is a hue chart for reference. My script replacecolor converts the input and output colors to hue (and brightness and saturation) and replaces hues.

See http://www.imagemagick.org/script/color.php and the link to Wikipedia regarding how hues are compute.

Post Reply