Fill behind all translucent (but not fully transparent) pixels

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
Pudlo
Posts: 2
Joined: 2019-09-05T07:20:08-07:00
Authentication code: 1152

Fill behind all translucent (but not fully transparent) pixels

Post by Pudlo » 2019-09-05T07:31:14-07:00

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!

snibgo
Posts: 12142
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2019-09-05T07:45:30-07:00

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

Pudlo
Posts: 2
Joined: 2019-09-05T07:20:08-07:00
Authentication code: 1152

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

Post by Pudlo » 2019-09-05T08:00:13-07:00

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

User avatar
fmw42
Posts: 25543
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2019-09-05T09:08:31-07:00

Note that Unix syntax is different from Windows syntax in that regard.

Post Reply