I've got a PNG image with transparency:
Now I want to use ImageMagick to apply a diagonal gradient to its alpha channel. I mean so that its opacity remains in the top left corner, and gradually fades out to completely transparent in the bottom right corner. Like this:
So basically I want to generate a gradient, and use that as a mask for the image. But the image already has an alpha channel (transparency) of its own. Here's a visualisation of what I'm trying:
(original and result here displayed on checkerboard for visiblity, but I mean actual transparency)
I think I understand how to generate a diagonal gradient (the barycentric gradient command is very useful for this). But this creates a gradient in the color channels i.e. a colored or grayscale gradient. Whereas I want to apply the gradient on the alpha channel.
From the IM manual I understand the -compose CopyOpacity
operator could be used for this. However this seems to copy the alpha from the mask on to my image. I need to "apply" this gradient color on my existing alpha channel, so basically I need my image's alpha channel to be multiplied by the grayscale color from the gradient image.
What would be the correct IM command line to perform the operation displayed above?
MPR:
and-fx
usage were new to me! Inspired by your solution I'm now doing it like this:convert tree.png \( +clone -alpha extract \( +clone -fx "1-(i+j*w/h)/(2*w)" \) -compose multiply -composite \) -compose CopyOpacity -composite result.png
where-fx "1-(i+j*w/h)/(2*w)"
creates the diagonal gradient – Analogy