Page 1 of 1

two images, if pixel in 2nd = 1st then make it transparent

Posted: 2009-08-22T15:17:12-07:00
by wsurowiec
Is this possible with ImageMagick:

I have two images, the first is only the background, the second includes my subject in the foreground.

On a pixel by pixel basis, if the two pixels have the same value can I turn the pixel transparent in the second or a resultant image?

I have full control in making the original images - lighting, focus, f stop, camera placement, exposure - with adequate physical and lighting separation between the background and foreground to avoid interactions.

What I do not have is a single pixel value for all the pixels in the background (an easy solution exists for this case), and providing a range, or a fuzz factor, unacceptably degrades the foreground subject (otherwise another easy solution.)

I understand I can work harder at controlling the uniformity of the background. I would like to know if I can make the computer work harder instead.

Bill

Re: two images, if pixel in 2nd = 1st then make it transparent

Posted: 2009-08-22T15:25:42-07:00
by fmw42
If the background and the foreground are the same size, then here is one way.

I have a white background and the logo: (internal IM image) that has a lot of white in it. I will make the white which matches the background in the resulting image of the logo become transparent.

convert \( -size 640x480 xc:white \) logo: \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 0 -compose copy_opacity -composite logo_with_white_transparent.png


This gets the difference image and thresholds it and put is as the alpha channel for the logo image.

Re: two images, if pixel in 2nd = 1st then make it transparent

Posted: 2009-08-23T01:51:00-07:00
by anthony
This is the compose operator ChangeMask! That is exactly what it does.
Though you can specify a -fuzz factor to also allow 'near equal' colors too. See examples at...
http://www.imagemagick.org/Usage/compose/#changemask

It was developed to allow me to create the GIF animation transparency optimization method -layers OptimizeTransparency
http://www.imagemagick.org/Usage/anim_opt/#opt_trans

Re: two images, if pixel in 2nd = 1st then make it transparent

Posted: 2009-08-23T06:18:33-07:00
by wsurowiec
Thank you Anthony and fmw42. I will try these approaches - I agree, they will work.

While I spent many hours in the documentation - even reading some of the above referenced material - the answer did not occur to me. Learning what to do - and what not to do - requires far more than learning how to do something. Without the sharing of knowledge that experience yields this would have been - at least for me - a far more difficult problem to solve, possibly unsolvable, given resource constraints and the uncertainty that the goal is obtainable. Again, thank you.

Bill