[SOLVED] Flatten image onto white but keep alpha channel

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
monark
Posts: 13
Joined: 2016-09-14T03:36:35-07:00
Authentication code: 1151

[SOLVED] Flatten image onto white but keep alpha channel

Post 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
Last edited by monark on 2016-10-24T02:26:34-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Flatten image onto white but keep alpha channel

Post 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.
monark
Posts: 13
Joined: 2016-09-14T03:36:35-07:00
Authentication code: 1151

Re: Flatten image onto white but keep alpha channel

Post 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
monark
Posts: 13
Joined: 2016-09-14T03:36:35-07:00
Authentication code: 1151

Re: Flatten image onto white but keep alpha channel

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