Vertical linear transparent gradient

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
mantafight
Posts: 3
Joined: 2017-10-09T10:41:03-07:00
Authentication code: 1151

Vertical linear transparent gradient

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Vertical linear transparent gradient

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Vertical linear transparent gradient

Post 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
mantafight
Posts: 3
Joined: 2017-10-09T10:41:03-07:00
Authentication code: 1151

Re: Vertical linear transparent gradient

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Vertical linear transparent gradient

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Vertical linear transparent gradient

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Vertical linear transparent gradient

Post 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.
snibgo's IM pages: im.snibgo.com
mantafight
Posts: 3
Joined: 2017-10-09T10:41:03-07:00
Authentication code: 1151

Re: Vertical linear transparent gradient

Post by mantafight »

thanks for the links and examples, your help is incredibly useful for everything I wanted to achieve!
Post Reply