Restore transparent background with a different image.

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
coolperez8
Posts: 27
Joined: 2016-03-11T07:27:11-07:00
Authentication code: 1151

Restore transparent background with a different image.

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

Re: Restore transparent background with a different image.

Post 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.
snibgo's IM pages: im.snibgo.com
coolperez8
Posts: 27
Joined: 2016-03-11T07:27:11-07:00
Authentication code: 1151

Re: Restore transparent background with a different image.

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

Re: Restore transparent background with a different image.

Post 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
snibgo's IM pages: im.snibgo.com
Post Reply