Page 1 of 1

Fill behind all translucent (but not fully transparent) pixels

Posted: 2019-09-05T07:31:14-07:00
by Pudlo
Hello!

Code: Select all

$ magick --version
Version: ImageMagick 7.0.8-62 Q16 x64 2019-08-24 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenCL OpenMP(2.0)
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr lzma openexr pangocairo png ps raw rsvg tiff webp xml zlib
Sorry if I do a poor job of explaining this.

I have a PNG with transparent and translucent pixels:

Image

I'd like to convert it to GIF, but "mogrify -format gif input.png" gives an ugly result:

Image

I'd like to fill behind all translucent (but not fully transparent) pixels with an opaque color (white), and then convert to get this result:

Image

Is there any way to do this in ImageMagick?

Thank you for your help!

Re: Fill behind all translucent (but not fully transparent) pixels

Posted: 2019-09-05T07:45:30-07:00
by snibgo
Pudlo wrote:I'd like to fill behind all translucent (but not fully transparent) pixels with an opaque color (white), ...
This does what you ask for (Windows syntax):

Code: Select all

magick j86x4XQ.png ( -clone 0 -background White -layers Flatten ) ( -clone 0 -alpha extract -threshold 0 ) -delete 0 -alpha off -compose CopyOpacity -composite filltrans.gif
We flatten the entire image against white, then we make fully transparent just those pixels that were originally fully transparent.
Image
There is a white halo around the circle because the input had anti-aliased alpha.

Re: Fill behind all translucent (but not fully transparent) pixels

Posted: 2019-09-05T08:00:13-07:00
by Pudlo
Hey, thanks so much! You're a wizard.

For anybody reading this in the future, I had to backslash escape the brackets:

Code: Select all

magick j86x4XQ.png \( -clone 0 -background White -layers Flatten \) \( -clone 0 -alpha extract -threshold 0 \) -delete 0 -alpha off -compose CopyOpacity -composite filltrans.png

Re: Fill behind all translucent (but not fully transparent) pixels

Posted: 2019-09-05T09:08:31-07:00
by fmw42
Note that Unix syntax is different from Windows syntax in that regard.