Search code examples
imagemagickimagemagick-convert

Gradient over transparent png image with sparse-color has background color


Source Image

I have this face source image. I'm tyring to add a gradient to the bottom of the picture so it fades out alpha transparent.

Problem is that the transparent source image is not transparent anymore after sparse-color transformation - the transparent area is now black.

This is my cmd so far:

magick convert face.png -alpha set -background none -channel A -sparse-color barycentric "0,%[fx:h*0.90] white 0,%[h] none" +channel face-gradient.png

Solution

  • Here is how you have to do that in Imagemagick. Your image already has an alpha channel. So you have to create a new grayscale gradient image as a mask and combine that with the existing alpha channel. (The -sparse-color is going to write over your existing alpha channel.)

    magick face.png \
    \( -clone 0 -alpha extract \) \
    \( -clone 0 -sparse-color barycentric "0,%[fx:h*0.90] white 0,%[h] black" \) \
    \( -clone 1,2 -compose multiply -composite \) \
    -delete 1,2 \
    -alpha off -compose copy_opacity -composite face-gradient.png
    

    enter image description here