Page 1 of 1

Create color mask for translucent figure

Posted: 2019-12-03T14:50:19-07:00
by shveicar
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

Re: Create color mask for translucent figure

Posted: 2019-12-03T15:51:35-07:00
by fmw42
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

Re: Create color mask for translucent figure

Posted: 2019-12-03T16:27:54-07:00
by shveicar
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

Re: Create color mask for translucent figure

Posted: 2019-12-03T16:29:00-07:00
by fmw42
Is that not what you want? That looks the same as my result. If it is not what you want, then please clarify further.

Re: Create color mask for translucent figure

Posted: 2019-12-03T16:42:41-07:00
by shveicar
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.

Re: Create color mask for translucent figure

Posted: 2019-12-03T19:42:05-07:00
by fmw42
Change red in my code to some dark blue color. See color names and values at https://imagemagick.org/script/color.php

Re: Create color mask for translucent figure

Posted: 2019-12-04T03:14:02-07:00
by shveicar
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