Page 1 of 1

how to do a transparent gradient?

Posted: 2008-01-17T16:04:32-07:00
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.

Re: how to do a transparent gradient?

Posted: 2008-01-17T16:58:08-07:00
by el_supremo
You need to use the double quote character around the cos function. "cos(pi*(i/w-0.5))"

Pete

Re: how to do a transparent gradient?

Posted: 2008-01-17T17:06:06-07:00
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

Re: how to do a transparent gradient?

Posted: 2008-01-18T02:22:35-07:00
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%] ?

Re: how to do a transparent gradient?

Posted: 2008-01-18T02:27:27-07:00
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.

Re: how to do a transparent gradient?

Posted: 2008-01-18T03:49:32-07:00
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.

Re: how to do a transparent gradient?

Posted: 2008-01-18T08:46:14-07:00
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?

Re: how to do a transparent gradient?

Posted: 2008-01-18T09:06:53-07:00
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

Re: how to do a transparent gradient?

Posted: 2008-01-18T10:36:14-07:00
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.

Re: how to do a transparent gradient?

Posted: 2008-01-18T10:55:06-07:00
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.

Re: how to do a transparent gradient?

Posted: 2008-01-18T13:55:00-07:00
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.

Re: how to do a transparent gradient?

Posted: 2008-01-23T23:43:05-07:00
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.