how to do a 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
zomp

how to do a transparent gradient?

Post by zomp »

In my last thread I asked a suggestion to draw a 3D cylinder. Bonzo, that I thank, has stimulated me to study the draw syntax. My solution is:

Code: Select all

convert -size 120x60 xc:none -fill none -stroke black -draw "roundrectangle 5,5,115,55 25,25" -draw "roundrectangle 5,5,55,55 25,25" cyl1.png
The result is ok for me.
Now, since I want to make the cylinder transparent, I need some type of gradient where one color is none (that is, transparent).

Unfortunately, it seems to me that I have not well understood how to use the gradient: option.

I tried without success the following command:

Code: Select all

convert cyl1.png gradient:none-white -flatten gradient.png
convert: Must specify image size `none-white'
and this other one:

Code: Select all

convert cyl1.png -fx 'cos(pi*(i/w-0.5))' -flatten gradient.png
convert: unable to parse expression `cos(pi*(i/w-0.5))'
(I work on a Windows XP machine, sorry)
Let me say that I copied the -fx argument from an example in page http://www.imagemagick.org/Usage/canvas ... ent_resize.
Even if I type the command exactly as it is reported in the example,

Code: Select all

convert  rose:  -fx 'cos(pi*(i/w-.5))'   gradient_fx_cos.gif
I obtain the same error message.

Furthermore, this example draws a horizontal symmetric gradient, even if I need a vertical one. Since it is not clear for me the meaning of i and w, I am not able to modify the formula inside parenthesis to obtain the effect I want.

Thanks for your help and your patience.

Your program is wonderful.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to do a transparent gradient?

Post by el_supremo »

You need to use the double quote character around the cos function. "cos(pi*(i/w-0.5))"

Pete
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to do a transparent gradient?

Post by el_supremo »

convert cyl1.png gradient:none-white -flatten gradient.png
convert: Must specify image size `none-white'
I tried that command using the logo: image (since I don't have your cyl1.png image) and it fails with the same message but:

Code: Select all

convert logo: -size 640x480 gradient:none-white -flatten gradient.png
Seems to work OK using ImageMagick 6.3.5 07/31/07 Q8 on Win SP Pro sp2.

Pete
zomp

Re: how to do a transparent gradient?

Post by zomp »

el_supremo wrote:
convert cyl1.png gradient:none-white -flatten gradient.png
convert: Must specify image size `none-white'
I tried that command using the logo: image (since I don't have your cyl1.png image) and it fails with the same message but:

Code: Select all

convert logo: -size 640x480 gradient:none-white -flatten gradient.png
Seems to work OK using ImageMagick 6.3.5 07/31/07 Q8 on Win SP Pro sp2.

Pete
Thanks for your help.
In your opinion, is it possible (and if yes, how) to obtain a gradient from [none] to [white50%-none50%] ?
zomp

Re: how to do a transparent gradient?

Post by zomp »

el_supremo wrote:You need to use the double quote character around the cos function. "cos(pi*(i/w-0.5))"

Pete
Thanks. It works even without quotes, as I have succeeded yesterday at very very late hour after many experiments. I do not know if that depends on my OS, but I write here hoping this can be useful to someone in the future.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: how to do a transparent gradient?

Post by Bonzo »

The gradiant none has not been around long and so you need a current version to use it.

I created my gradiant and then rotated it through 90 degrees before using it.
zomp

Re: how to do a transparent gradient?

Post by zomp »

zomp wrote:Furthermore, this example draws a horizontal symmetric gradient, even if I need a vertical one. Since it is not clear for me the meaning of i and w, I am not able to modify the formula inside parenthesis to obtain the effect I want.
Solved. I have to use j and h in place of i and w.

Now the only one question to answer is: is it possible to make a gradient from none to a half-transparent color?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to do a transparent gradient?

Post by el_supremo »

You can use the rgba function to specify a partially transparent colour like this:

Code: Select all

convert -size  256x256 gradient:none-rgba(100%,0,0,0.5) gradient.png
Note that the Alpha component is a fractional value from zero to one.

Pete
zomp

Re: how to do a transparent gradient?

Post by zomp »

el_supremo wrote:You can use the rgba function to specify a partially transparent colour like this:

Code: Select all

convert -size  256x256 gradient:none-rgba(100%,0,0,0.5) gradient.png
Note that the Alpha component is a fractional value from zero to one.

Pete
Unfortunately, all my trials to work with -fx and -flatten options had no success!

For example, the following command:

Code: Select all

convert -size 120x60 -fx cos(pi*(j/h-0.5)) gradient:none-white  gradient.png
draws a gradient from N to S but not centered to the half height of the image, and from none to black instead of from none to white!

Similar result for various trials with -flatten option.

Maybe I have choosen a roundabout way.

Can you suggest me a clear command to obtain a png image with gradient varying from trasparency at the median height to some color at the top and the bottom of the image?

Thanks for your patience.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: how to do a transparent gradient?

Post by Bonzo »

I see what you mean about the gradiant it seems to be more transparent than colour.

All I can think of is make the gradiant image larger and crop it.
zomp

Re: how to do a transparent gradient?

Post by zomp »

Bonzo wrote:I see what you mean about the gradiant it seems to be more transparent than colour.

All I can think of is make the gradiant image larger and crop it.
Yes, this is a smart suggestion.

But I have great problem first: I am not able to use -fx option to yield a gradient from some color to none or viceversa. All the temptatives I have made yield an image with black-white gradient. I have read that -fx option accepts also colour codes, but I have not found an example useful to adapt to my case.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to do a transparent gradient?

Post by anthony »

One work -flatten will always start with an image that is the current background color. That is usally an opaque color, so when your image is -flatten'ed you will lose all transparency.

Set -background none first if using -flatten and want to preserve transparency. -flatten with one image and a background of 'none' is only useful when wanting to 'fill out' a virtual canvas which I don't think you are doing.

It is normally a layering operator. not a gradient handling operator. But that 'background' part makes it useful for removing transparency.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply