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

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
wsurowiec

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

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

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia
Contact:

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
wsurowiec

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

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

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests