Apply gradient mask on image

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
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Apply gradient mask on image

Post by myspacee »

Hello,
i want to learn how apply gradient mask on image.

My goal is, for given input image, reach image like this:
Image

I'm using windows version, so help me with syntax please.

Thank you for your time,
(and sorry for english)

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

Re: Apply gradient mask on image

Post by snibgo »

What version IM? Please supply the input image.

Assuming the input is an opaque tree with transparent background: extract the alpha, create a horizontal gradient of the same size with black on the left, multiply the extracted alpha by the gradient, CopyOpacity this back to the alpha channel.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Apply gradient mask on image

Post by GeeMack »

myspacee wrote: 2017-06-16T13:18:15-07:00i want to learn how apply gradient mask on image. [...] I'm using windows version, so help me with syntax please.
Using IM 6.9.7-6 HDRI on Windows 10, and running this in a CMD window, I get the result you described.

Code: Select all

convert input.jpg -alpha set -background none -channel A ^
   ( +clone -sparse-color barycentric "0,0 white %[w],0 none" ) -compose copyopacity -composite output.png
That reads the input image, sets the alpha channel to active, sets a background color of "none", and sets the active channel to "A" so it runs the following operations on the alpha channel.

Then it clones the input image and colors that clone with a gradient going from white on the left to transparent on the right. That makes a mask the same dimensions as your input image.

Then it sets a compose method of "copyopacity" and composites the mask over the input image. It essentially displays your input from 100% where the mask is white down to 0% where the mask is transparent.

This method makes it easy to modify the start and end points for the gradient. You could make the fade start one third in from the left side and run just another third across the width with something like this...

Code: Select all

... ( +clone -sparse-color barycentric "%[fx:w*0.33],0 white %[fx:w*0.66],0 none" ) ...
Or make the gradient run corner to diagonal corner like this...

Code: Select all

... ( +clone -sparse-color barycentric "0,0 white %[w],%[h] none" ) ...
The same command should work in IM7 by using "magick" instead of "convert".

To run this from a BAT script you'd need to change the single percent signs "%" to doubles "%%".
Post Reply