Page 1 of 1

[SOLVED] Flatten image onto white but keep alpha channel

Posted: 2016-10-14T11:59:01-07:00
by monark
I have a bunch of png files with transparency which are premultiplied with black.
I need them to be composited on white but preserve the alpha channel for converting to webp format.
If I don't do that I get a black edge where there are semi transparent pixels.

Is there a simple command that can composite an image onto white but preserve the alpha channel?

I've found that this does the first part nicely and correctly handles the premultiplied alpha

magick source.png -background white -alpha remove out.png

but how do you preserve the alpha channel during the operation?

Is the only way to do it to copy it out into a file first and then composite it back, i.e using 3 operations
Can it be done in one?

Another approach would be to unmultiply the alpha but I can't seem to find anything in magick that will do that.

If it helps this is the full set of steps that I'd like to reduce if possible

magick -alpha extract source.png mask.png
magick source.png -background white -alpha remove flatten.png
magick flatten.png mask.png -alpha off -compose copy_opacity -composite -background white -alpha background final.png

Re: Flatten image onto white but keep alpha channel

Posted: 2016-10-14T14:07:47-07:00
by fmw42
First, please always provide your IM version and platform when asking questions since syntax may vary. Please upload and example image to some place such as dropbox.com and put the URL here so we can see what you are describing and test.

Have you tried (http://www.imagemagick.org/Usage/maskin ... background)

Code: Select all

magick source.png -background white -alpha background result.png
Note proper IM 6 and 7 syntax reads a raster image before any processing.

Re: Flatten image onto white but keep alpha channel

Posted: 2016-10-17T02:18:01-07:00
by monark
So it looks from this link like what I need to use is the clone operator

http://www.imagemagick.org/Usage/basics/#clone

Re: Flatten image onto white but keep alpha channel

Posted: 2016-10-17T02:51:08-07:00
by monark
Yup so this line does everything my 3 originals did

magick source.png \( +clone -background white -alpha remove \) -compose Over -composite final.png