Create color mask for translucent figure

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
User avatar
shveicar
Posts: 30
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Create color mask for translucent figure

Post by shveicar » 2019-12-03T14:50:19-07:00

Hello
Please tell me a convenient way to create a color mask for a given shape.
For example I have this image:

Image

On the output I should get this image:

Image

I chose black, but sometimes it can be another colors.
Thanks

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

Re: Create color mask for translucent figure

Post by fmw42 » 2019-12-03T15:51:35-07:00

Try this:

Code: Select all

convert img.png -alpha extract -background red -alpha shape mask.png
If on IM 7, use magick rather than convert.

Please always provide your IM version and platform when asking questions.

See https://imagemagick.org/Usage/masking/#alpha_shape

User avatar
shveicar
Posts: 30
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Create color mask for translucent figure

Post by shveicar » 2019-12-03T16:27:54-07:00

fmw42
Thanks for the examples. I have different versions for checking commands on Windows system. the main now version ImageMagick of 7.0.8
But unfortunately, I didn't find a suitable example for this type of image there.
with your example, I get this image output:

Image

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

Re: Create color mask for translucent figure

Post by fmw42 » 2019-12-03T16:29:00-07:00

Is that not what you want? That looks the same as my result. If it is not what you want, then please clarify further.

User avatar
shveicar
Posts: 30
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Create color mask for translucent figure

Post by shveicar » 2019-12-03T16:42:41-07:00

With your example I get a shape with a changed color instead of the black shape given above.
I need to get the same shape

Image

only for example dark blue.

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

Re: Create color mask for translucent figure

Post by fmw42 » 2019-12-03T19:42:05-07:00

Change red in my code to some dark blue color. See color names and values at https://imagemagick.org/script/color.php

User avatar
shveicar
Posts: 30
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Create color mask for translucent figure

Post by shveicar » 2019-12-04T03:14:02-07:00

I tested several options that could create such an image from the original using ImageMagick. As a result I have created a rather long way of such conversion:

Code: Select all

convert -size 600x600 xc:red ( img-in.png -background black -flatten +repage -rotate 180 +repage -fill none -fuzz 1% -draw matte 0,0 floodfill -flop -draw "matte 0,0 floodfill" -flop -rotate 180 +repage -fill none -fuzz 1% -draw "matte 0,0 floodfill" -flop -draw "matte 0,0 floodfill" -flop +repage ) -gravity center -compose Over -composite "C:\temp\temp.png"
convert -size 600x600 xc:red "C:\temp\temp.png" -compose ChangeMask -composite -fuzz 1% -trim +repage "C:\temp\mask.png"
convert -size 600x600 xc:NavyBlue -write mpr:temp ( mpr:temp "C:\temp\mask.png" -gravity center -compose Over -composite +repage ) -compose ChangeMask -composite -fuzz 1% -trim +repage "C:\temp\output.png"
at the output I get what I need:

Image

But perhaps some of the experts know a more simpler solution?

Image

Post Reply