Page 1 of 1

Restore transparent background with a different image.

Posted: 2019-04-03T06:00:39-07:00
by coolperez8
So, I have two images that are nearly identical, the alpha channel should be the same between both images, but one of the images has a white background and the character has a different color, while the other image has a transparent background. Other than that, the alpha channel should be the same between both images.

I've seen a method of how to restore a transparent background with two of the same image when one of the backgrounds is white and the other one is black, but I was wondering if it's possible to restore a transparent background to an image where you have one transparent and the alpha channel should be the same, but for instance, the character's clothes are a different color. Simply copying the alpha channel to the other image doesn't work because it leaves "white residue" from the background in semi-transparent areas.

If possible, would there be a method to restore it if a semi-transparent area is a different color in the image?

Re: Restore transparent background with a different image.

Posted: 2019-04-03T06:23:15-07:00
by snibgo
What version of IM? On what platform?

Please supply sample inputs and desired output.

My pages Inverse composites and Watermarks may be relevant.

Re: Restore transparent background with a different image.

Posted: 2019-04-03T07:27:34-07:00
by coolperez8
Image Magick is version 7.0.7-Q16 on 64-bit Windows

Sample input (Transparent)
Image

Sample Input (Background)
Image

Sample Output
Image

Re: Restore transparent background with a different image.

Posted: 2019-04-03T09:11:28-07:00
by snibgo
I've named your "Sample input (Transparent)" as "intrans.png".

I've named your "Sample Input (Background)" as "inback.png". This is fully opaque. The two images are the same size.

I think you want this: copy the alpha channel from intrans.png to inback.png. That's easy: "-compose CopyOpacity -composite". That is fine where alpha is 0.0 or 1.0. The problem is where pixels are partially transparent.

We can regard inback.png as some unknown image inxx.png (that has transparency) flattened (with "-compose Over") against an opaque white background. The task is to find image inxx.png.

Watermarks gives a solution.

First, make an image of the opacity:

Code: Select all

magick intrans.png -alpha extract inalpha.png
Image

... and a white version:

Code: Select all

magick intrans.png -fill White -colorize 100 inwhite.png
Calculate the unknown image inxx.png:

Code: Select all

call %PICTBAT%findWater2 inwhite.png inback.png inalpha.png inxx.png
Image