jeevcat
jeevcat

Reputation: 363

ImageMagick: Replace RGB channels with white, retain Alpha

I have an image created in a Affinity Designer, which has the following channels:

R, G and B:

RGB channels

Alpha:

Alpha channel

When I use this image in other places (Unreal Engine), I get small black artifacts on the edge of the shape due to the black part of the RGB channels. The only important information I need is the alpha channel.

How can I use ImageMagick to replace the RGB channels with white, while retaining the alpha channel?

Upvotes: 2

Views: 1006

Answers (2)

fmw42
fmw42

Reputation: 53237

Here is a simple way to do the in ImageMagick:

First, since you did not provide the actual image, I will reconstruct it from the rgb.png and alpha.png

convert rgb.png alpha.png -alpha off -compose copy_opacity -composite image.png


Now, to make the rgb part white, simply do:

convert image.png -fill white -colorize 100 result.png


Now to see that the rgb parts are fully white, we do:

identify -verbose result.png
.
.
.
 Channel statistics:
    Pixels: 512
    Gray:
      min: 255  (1)
      max: 255 (1)
      mean: 255 (1)
      standard deviation: 0 (0)
      kurtosis: -4.9152e+52
      skewness: 9.375e+35
      entropy: 0
    Alpha:
      min: 0  (0)
      max: 255 (1)
      mean: 187.152 (0.733931)
      standard deviation: 109.252 (0.42844)
      kurtosis: -0.822401
      skewness: -1.05578
      entropy: 0.336307


So the rgb components are a constant 255 (white) and the image is now white with an alpha component.

Upvotes: 4

emcconville
emcconville

Reputation: 24439

Try the following...

magick \( input.png -fill white -draw 'color 0,0 reset' \) \
       \( input.png -alpha extract \) \
       -compose Copy_Alpha -composite \
       output.png

I'm sure there's better ways to do the above. Reading the input.png twice, and extracting the alpha channel is bit redundant.

The first part replaces all RGB channels with white, but also clears the alpha channel.

input.png -fill white -draw 'color 0,0 reset'

The second part grabs the alpha channel.

input.png -alpha extract

Finally, we copy the extracted alpha values back over the white image's alpha channel.

-compose Copy_Alpha -composite

If your using ImageMagick-6, then replace magick with convert, and Copy_Alpha with Copy_Opacity.

Best of Luck!

Upvotes: 2

Related Questions