Making semi-transparent white pixels in an image all a single color

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
nihilazo
Posts: 2
Joined: 2018-10-18T08:42:35-07:00
Authentication code: 1152

Making semi-transparent white pixels in an image all a single color

Post by nihilazo »

Hello, I have some images like these ones:
Image
Image

I would like to make the figures on the images (which are currently semi-transparent) be totally opaque, and go from the greyish color they are to being totally white (#ffffff). How would I go about this using convert? All of the images also have these different amounts of alpha on the figures, so I can't do a blind one-color-for-another replace.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Making semi-transparent white pixels in an image all a single color

Post by GeeMack »

nihilazo wrote: 2018-10-18T09:03:44-07:00I would like to make the figures on the images (which are currently semi-transparent) be totally opaque, and go from the greyish color they are to being totally white (#ffffff). How would I go about this using convert? All of the images also have these different amounts of alpha on the figures, so I can't do a blind one-color-for-another replace.
There are probably a few good ways to do this. My first thought was to threshold the alpha channel, and fill the positive with white. A command like this gets me pretty close to what you described...

Code: Select all

convert input.png -channel A -threshold 10% -channel RGB -fill white -colorize 100 output.png
You might be able to tweak the threshold percentage so it accommodates the range of alpha in your various images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making semi-transparent white pixels in an image all a single color

Post by fmw42 »

Your alpha channel is the same as your base image. You can see that from comparing the results of

convert test.png -alpha off aoff.png
Image


convert test.png -alpha extract alpha.png
Image



My thought was to make the base image pure white and then threshold the alpha channel.

Image

Code: Select all

convert test.png -channel rgb -evaluate set 100% +channel -channel a -threshold 50% +channel test1.png
Image

Looking at GeeMack's solution, it appears to be similar but in the reverse order.

I thresholded the alpha channel more, so that the black lines in the image showed as transparency. If you do not want those outlines, then threshold at 0% (as user snibgo suggested below).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Making semi-transparent white pixels in an image all a single color

Post by snibgo »

As GeeMack says. To change "partially transparent" to "fully opaque", while leaving fully transparent unchanged, use "-threshold 0".
snibgo's IM pages: im.snibgo.com
nihilazo
Posts: 2
Joined: 2018-10-18T08:42:35-07:00
Authentication code: 1152

Re: Making semi-transparent white pixels in an image all a single color

Post by nihilazo »

Thank you everyone! This works perfectly.
Post Reply