Replace/Copy by Color 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
Arida
Posts: 2
Joined: 2017-01-19T06:52:40-07:00
Authentication code: 1151

Replace/Copy by Color Mask

Post 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)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace/Copy by Color Mask

Post 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.
snibgo's IM pages: im.snibgo.com
Arida
Posts: 2
Joined: 2017-01-19T06:52:40-07:00
Authentication code: 1151

Re: Replace/Copy by Color Mask

Post 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.
Post Reply