Trying to colourise or combine through an alpha (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
LDAsh
Posts: 35
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

Trying to colourise or combine through an alpha (mask)

Post by LDAsh »

It's been a few days now and I still can't figure this out.
"Version: ImageMagick 6.9.0-1 Q16 x86 2014-12-22"

Image
+
Image
=
Image

I want to make pixels red where the white alpha pixels are.
I would also settle for making a copy of in_pat.png that has been colourised red, then using a "pattern fill" to insert it into the original where the white alpha is.

I can't really post everything I've tried because it would be a total mess, but for example:-

Code: Select all

convert in_pat.png -mask in_mask.png -colorspace sRGB -fill "rgb(255,0,0)" -tint 100 +mask png32:out!!!.png

Code: Select all

convert in_pat.png -mask in_mask.png +level-colors 'rgb(100%%,0%%,0%%)',red -colorize 100%% +mask png32:out!!!.png

Code: Select all

convert in_pat.png -mask in_mask.png -fill "rgb(255,0,0)" -tint 100 +mask png32:out!!!.png
And a combination of applying the alpha channel before these commands. I can either get the black alpha to red or white alpha to red, but the rest of the image is black?

Any help would be extremely appreciated...
.
.
.

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

Re: Trying to colourise or combine through an alpha (mask)

Post by snibgo »

The first step is to find the command that makes a red version of the pattern image in_pat.png, for the entire image. The input has black, and gray(50%), and anti-aliasing. You want to leave black unchanged, and gray(50%) to be changed to #ea0e0e, more or less. "-auto-level" stretches the values to black to white, and "+level-colors black,#ea0e0e" transforms to the required colours.

Okay, we want the change only where in_mask.png is white, and no change where in_mask.png is black. We can do that by compositing the change over the original, using the mask image.

So the final command is:

Code: Select all

magick in_pat.png ( +clone -auto-level +level-colors black,#ea0e0e ) in_mask.png -compose Over -composite out.png
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 753
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trying to colourise or combine through an alpha (mask)

Post by GeeMack »

LDAsh wrote:
2020-01-25T09:16:45-07:00
I want to make pixels red where the white alpha pixels are.
The command in snibgo's reply is an excellent simple approach. Here are a couple other ideas to consider....

First, if your masked area is always a rectangle, you could use "-region" instead of a mask image to specify an area. Then you'd modify just that area to get the red. This command is an example...

Code: Select all

convert in_pat.png -region 128x128+322+190 -auto-level +level-colors black,red +region out_region.png
That reads the input image, describes your mask area as a region like WxH+x+y, adjusts the levels in that area to span black to white, then "+level-colors" essentially fills in red where the white is. This won't be helpful if your required masked area isn't a simple rectangle.

Another approach is to create a third layer of just all red, then use a composition method of "overlay" with that red image and your mask image to create the output like this...

Code: Select all

convert in_pat.png ( +clone -fill red -colorize 100 ) in_mask.png -compose overlay -composite out_test2.png
Like the approach snibgo suggested, this method will also work with irregular or gradient masked areas. Either method should produce a result very much like what you describe.

LDAsh
Posts: 35
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

Re: Trying to colourise or combine through an alpha (mask)

Post by LDAsh »

Thanks a lot! So I should give up trying to use "mask" and just look into manipulating whole images (copies) and then composing those in.

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

Re: Trying to colourise or combine through an alpha (mask)

Post by snibgo »

Problems like "apply some effect to some portion of the image" can be broken down into:

1. What effect do I want? Eg make pixels redder.

2. Where do I want the effect? Eg in a rectangle or only where the input image is dark.

3. Is the effect to be binary (fully on in some pixels, and fully off in the rest) or graduated (fully on, fully off, or partial).

A masked composite caters for all situations. Clone the image, apply the effect to the clone, and composite over the input with a mask.

"-region" caters for only the situation of binary effects in a rectangular area.

There is also "-read-mask" and "-write-mask", though I rarely use those.
snibgo's IM pages: im.snibgo.com

Post Reply