0

I have two images, one with a white background, and one with a black background, I want to convert these images into one semi transparent image

I have found that

color*alpha=blackBackgroundImage
color*alpha+(1-alpha)=whiteBackgroundImage

I want a function that takes blackBackgroundImage and whiteBackgroundImage and outputs the color and the alpha

I'm bad at math

william malo
  • 2,480
  • 2
  • 18
  • 18

1 Answers1

1

You're almost there. You just need to solve the system of equations:

    color*alpha=blackBackgroundImage
<=> color = blackBackgroundImage / alpha

Plugging that into the second equation:

    blackBackgroundImage / alpha * alpha + (1 - alpha)*(1,1,1) = whiteBackgroundImage
<=> blackBackgroundImage + (1 - alpha)*(1,1,1) = whiteBackgroundImage
<=> (1 - alpha)*(1,1,1) = whiteBackgroundImage - blackBackgroundImage

So alpha should be 1 + blackBackgroundImage.r - whiteBackgroundImage.r. The same applies to any other channel.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70