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 » 2017-10-09T10:54:19-07:00

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

Re: Vertical linear transparent gradient

Post by snibgo » 2017-10-09T13:06:15-07:00

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: 22101
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Vertical linear transparent gradient

Post by fmw42 » 2017-10-09T15:50:54-07:00

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 » 2017-10-09T16:00:54-07:00

@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: 22101
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Vertical linear transparent gradient

Post by fmw42 » 2017-10-09T16:06:21-07:00

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: 22101
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Vertical linear transparent gradient

Post by fmw42 » 2017-10-09T16:14:27-07:00

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

Re: Vertical linear transparent gradient

Post by snibgo » 2017-10-09T16:23:48-07:00

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 » 2017-10-09T16:31:45-07:00

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

Post Reply