Page 1 of 1

Replace/Copy by Color Mask

Posted: 2017-01-19T07:35:50-07:00
by Arida
Hello.
I need to partially replace first image by second. Both have transparency. But second image have build-in mask which is flat color (for example magenta). This part should stay untoched from first image and all other must be completely replaced (including transparency). I've tried many different methods but none give correct result.

Most closely to ideal result is:
convert i0.png i1.png -fx "u[1].r==1 && u[1].g==0 && u[1].b==1 && u[1].a==1 ? u[0] : u[1]" r.png
But it very slow and dispose of transparent color which make edges little sharper.

Also simple:
convert i0.png i1.png m.png -compose copy -composite r.png
(m.png is extracted mask from second image)
Doesn't copy alpha channel from second image.... Isn't it should? ("This operator is also very similar to 'Over', except that the transparency of the source image is also copied, replacing the background image completely.")

Is there a correct solution for this? (ImageMagick-7.0.4-Q16 Win7)

Re: Replace/Copy by Color Mask

Posted: 2017-01-20T05:48:25-07:00
by snibgo
This works as expected in v6.9.5-3, but not v7.0.3-6.

Create input files:

Code: Select all

%IM%convert -size 200x200 xc:Red gradient: -alpha off -compose CopyOpacity -composite tm_a.png
Image

Code: Select all

%IM%convert -size 200x200 xc:Green gradient: -alpha off -compose CopyOpacity -composite -rotate 90 tm_b.png
Image

Code: Select all

%IM%convert -size 200x200 xc:white -fill Black -draw "circle 100,100 150,100" tm_m.png
Image

Composite with v6:

Code: Select all

%IM%convert tm_a.png tm_b.png tm_m.png -compose Copy -composite tm_out6.png
Image

Composite with v7:

Code: Select all

%IMG7%magick tm_a.png tm_b.png tm_m.png -compose Copy -composite tm_out7.png
Image
This looks like a v7 bug to me.

Re: Replace/Copy by Color Mask

Posted: 2017-01-20T10:02:48-07:00
by Arida
Thanks, snibgo.
ImageMagick-6.9.7-Q16 did the trick. Copy method worked as expected. Since it's better and faster than "fx" i'll return to v6.