Page 1 of 1

Vertical linear transparent gradient

Posted: 2017-10-09T10:54:19-07:00
by mantafight
hello, i'm trying to make a vertical gradient that goes from black to transparent to black

so far, I've been using this command:

Code: Select all

convert -size '1280x720' gradient: -function Polynomial -4,4,0 -evaluate Pow 0.9 gradient_test_01.jpg
which goes from black-white-black because of the -function, but I can't find a way to make the middle transparent instead of white.
changing `gradient:` from default to have 'none' always makes the image black

does anyone know how I could make it black-transparent-black, or possibly any alternative ways to make such a gradient? I would also like to have some minor control over how far the 'transparent' part spreads out, which i can kind of do with the Polynomial coefficients

thanks!

Re: Vertical linear transparent gradient

Posted: 2017-10-09T13:06:15-07:00
by snibgo
When we have a grayscale image that represents opacity, where black represents transparent and white represents opaque, we can "CopyOpacity" to convert that gray into opacity.

For example:

Code: Select all

%IM%convert -size 1280x720 gradient: -function Polynomial -4,4,0 -evaluate Pow 0.9 +write g0.png ( +clone -fill Black -colorize 100 ) +swap -alpha off -compose CopyOpacity -composite out.png

Re: Vertical linear transparent gradient

Posted: 2017-10-09T15:50:54-07:00
by fmw42
If you want a linear gradient, try the following:

Code: Select all

convert -size 1280x720 gradient:black-none grad.png
Note: Gradients have been improved in later versions of Imagemagick. See http://www.imagemagick.org/script/gradient.php

Re: Vertical linear transparent gradient

Posted: 2017-10-09T16:00:54-07:00
by mantafight
@snibgo,
thanks so much! your example got me there, all I had to really do was negate the starting image so I could make the right part transparent

I ended up with:

Code: Select all

convert -size 1280x720 gradient: -function Polynomial -4,4,0 -evaluate Pow 0.9 -negate +write g0.png \( +clone -fill Black -colorize 100 \) +swap -alpha off -compose CopyOpacity -composite out.png
and it comes out nicely!

just wondering, do you know of any shorthand methods to construct a linear gradient similar to this one? i've looked through the documentation a bunch and this was the only way I could come up with it

@fmw42,
thanks for the suggestions,
I was initially working with the code snip you posted, but I was struggling trying to get it only black on the top and bottom, and transparent in the middle. My initial solution was performing your command twice with each image being 360px in height, then flipping and stacking one of them to get the desired output. Not sure which solution is most efficient

Re: Vertical linear transparent gradient

Posted: 2017-10-09T16:06:21-07:00
by fmw42
Sorry, I did not see where you said you wanted it transparent in the middle only.

This would seem to be the simplest, unless you wanted it curved rather than two linear segments.

Code: Select all

convert -size 1280x360 gradient:black-none \( +clone -flip \) -append grad.png

Re: Vertical linear transparent gradient

Posted: 2017-10-09T16:14:27-07:00
by fmw42
You can also do this:

Code: Select all

convert -size 1280x720 xc:black \
\( -size 1280x720 gradient: -solarize 50% -level 0x50% -negate \) \
-alpha off -compose copy_opacity -composite grad2.png

Re: Vertical linear transparent gradient

Posted: 2017-10-09T16:23:48-07:00
by snibgo
mantafight wrote:just wondering, do you know of any shorthand methods to construct a linear gradient similar to this one?
My Gradients Cookbook contains about 100 examples. They are mostly two-dimensional gradients, ie there are gradients in both axes.

In your case, every column is identical, so this is a one-dimensional gradient. You can imagine a graph showing intensities (or opacities) along that dimension. See Clut cookbook for about 400 examples.

EDIT: My "+write g0.png" was so you can see the intermediate step by viewing g0.png. You don't need this in your final command.

Re: Vertical linear transparent gradient

Posted: 2017-10-09T16:31:45-07:00
by mantafight
thanks for the links and examples, your help is incredibly useful for everything I wanted to achieve!