Text with gradient stroke?

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?".
josephtura

Text with gradient stroke?

Post by josephtura »

Hi everyone,

I am trying to create a text with a gradient stroke. Can that actually be done? I have managed to do the text with gradient by using -tile, but the stroke...

I have also tried drawing the text with a gradient a couple of times with different offsets and then drawing the actual text on top, but the results were somewhat angled, not like the nice example you can find on Anthony's example page... square dots on the 'i's don't seem to work out so nicely...

Does anyone have any ideas on that?

Oh, and another question: is letter spacing implemented in the latest version of IM?

TIA
jt
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

The tilesing is only used for the 'fill' color. However by drawing just the stroke on a transparent canvas, you can use that as a mask for the gradient image in the overlay.
It is a lot more work, but quite doable.

If you have an example of what you are workign with, I'll see if I can get it right for you.

The 'offset outlining' was provided more as a demonatration. It may for example be the olnly way of generating outlines for general 'shapes' rather than a drawable font.

You should be able to remove the square-ness by using a 12 offset scheme. An 8 offset scheme is shown, so it is a matter of uping the number of overlays.

What do you mean 'letter spacing'?
Note that currently full justification is not provided, though some thoughts have arisen in this regard, but with image appending. See IM Examples development, future proposals.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
josephtura

Post by josephtura »

Hi Anthony,

thank you for your reply. If I understand you correctly I should draw the stroke in a solid color on a transparent background and use that to mask a gradient?

I tried this, it seems to work. Can that be done in one step? And can the gradient color distribution be changed?

/usr/bin/convert -size 150x150 null: -compose Clear -composite -quality 90 -font fonts/DINPro-Black.otf -pointsize 48 -fill none -stroke black -strokewidth 6 -annotate +50+50 'Test' text.png
/usr/bin/convert -size 150x150 gradient:#cc0000-#ffffff gradient.png
/usr/bin/composite -compose Dst_In text.png gradient.png -matte combined.png
/usr/bin/convert -size 150x150 null: -compose Clear -composite -quality 90 -font fonts/DINPro-Black.otf -pointsize 48 -fill blue -annotate +50+50 'Test' text.png
/usr/bin/composite -compose Dst_Over -gravity center combined.png text.png combined.png

Cheers,
jt
Image
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Text with gradient stroke?

Post by rmabry »

josephtura wrote: I am trying to create a text with a gradient stroke. Can that actually be done?


There is actually code in ImageMagick that lets the stroke come from any image or pattern, just as -tile can be used with fills. (Check out instances of draw_info->stroke_pattern, esp. in the file draw_private.h, at least in the last few releases.) I presume that the gurus-at-large are waiting for some special moment to make that method available. It can probably already be used with Magick++ and such, but it doesn't seem to be available otherwise. (Or else I have missed the instrux for using it from the command-line and PerlMagick.)

I'm waiting for something like,

Code: Select all

convert image.png -pointsize 200 -strokewidth 10 -stroketile tile.png -draw "text 10,10 somewords" out.png
You'll be able to use gradients and patterns as well.

I'll bet if you ask nicely, The Wizard would make this available in the next release (or tell us how we can already start using it).

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

Post by el_supremo »

I've combined the commands so that there are now just two of them. I haven't tried crunching them down to one yet.

Code: Select all

convert -compose Dst_In ( -size 150x150 gradient:#cc0000-#ffffff ) ( -size 150x150 null: -quality 90 -font fonts/DINPro-Black.otf \
    -pointsize 48 -fill none -stroke black -strokewidth 6 -annotate +50+50 "Test" ) -composite combined.png

convert -compose Dst_Over ( -size 150x150 null: -quality 90 -font fonts/DINPro-Black.otf -pointsize 48 -fill blue -annotate +50+50 "Test" )  \
    combined.png -composite combined_out.png
Note to the magicians. While combining the commands I had problems converting a composite command to a -composite operator in the convert command until I figured out that the order of the operands is reversed. These two commands produce the same image:

Code: Select all

convert -compose Dst_Over text2.png combined.png -composite combined_out_conv.png

composite -compose Dst_Over combined.png text2.png combined_out_comp.png
This is with ImageMagick 6.3.1 01/12/07 Q8.

Pete
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

They are reverse due to historical and logical reasons...

In "composite" the images are source the destination as operators as that is the order that the compose methods are defined for. EG

Code: Select all

source ---over--> destination
.

However in "convert" you read in images 'to be applied to' first, then you want to overlay images on that. EG:

Code: Select all

main_image 'process image'  'overlay a new image over the working image'
They are just different ways of thinking, resulting in different argument orders, whcih are logical for their environments.

Remember "convert" uses a 'reverse polish' or 'do operations as you see them' type handler.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

Ah yes. Thanks Anthony.

Pete
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Text with gradient stroke?

Post by rmabry »

rmabry wrote: I'll bet if you ask nicely, The Wizard would make this available in the next release


See what I mean?

http://redux.imagemagick.org/discourse- ... 5429#25429

Rick
froesi

Re: Text with gradient stroke?

Post by froesi »

Hi guys,

is there already an easy way to do a gradient stroke?

I mean something like this:

-font Arial -pointsize 47 -fill none -stroke gradient:#FFFFFF-#000000 -strokewidth 5 . . .

thanks in advance.

cheers
froesi
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Text with gradient stroke?

Post by rmabry »

This seems to work they way you want:

convert -size 600x100 xc:white -font Arial -pointsize 80 -fill gradient:#FFFF00-#ff0000 -strokewidth 5 -draw "text 20,80 GRADSTROKE" gradstroke.png

This is not quite the way things were described in the post referred to in the post previous to yours (see proposed "stroke-pattern", etc.). However, I'm using 6.3.7 11/21/07 Q16, so I am a bit out-of-date (on WinXP).

Rick
froesi

Re: Text with gradient stroke?

Post by froesi »

Hi,
thank you rmabry. I updated the version and now its working. But now I've another Problem.
I've a big image, 1024x768 for example. In that image I put text, which should have this gradient stroke.
The problem is, I see that the gradient will be done on the text, but not really on the text. the gradient lies over the whole image, and starts at 0,0. How can I say, that the gradient only lies on the text.
In you example you have a 600x100 big image und your text starts at 20,80. Put your text to 20, 500 and you'll see what I mean. Is it possible to fix in one step, command?

thanks for your help
cheers
froesi
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Text with gradient stroke?

Post by rmabry »

Hi froesi,

I think I see what you mean. Try variants of the following. Here it is with a 20-pixel height for the gradient:

Code: Select all

convert -size 600x100 xc:white -font Arial -pointsize 80  ( -size 20x20 gradient:#FFFF00-#ff0000 -write mpr:grad ) -fill mpr:grad -strokewidth 5 -draw "text 20,80  GRADSTROKE" -delete 1 gradstroke.png 
Notice that mpr: is an pseudo-format for specifying an internal image, not written to disk. So this should be fast. The -delete operator pulls the temporary file off the stack, so only the main image is written. (Try leaving off -delete 1 to see what happens.) The parenetheses group commands to operate on a separate image. Note: the spaces are not optional around the parentheses!

Here is another type of trick for creating gradient-like images:

Code: Select all

convert -size 600x100 xc:white -font Arial -pointsize 80  ( -size 3x3  xc:blue -fill red -draw "color 1,1 point" -resize 20x20 -write mpr:grad ) -fill mpr:grad -strokewidth 5 -draw "text 20,80  GRADSTROKE" -delete 1 gradstroke.png
or

Code: Select all

convert -size 600x100 xc:white -font Arial -pointsize 80  ( -size 3x3  xc:yellow -fill blue -draw "color 0,1 point color 2,1 point" -fill green -draw "color 1,0 point color 1,2 point" -fill red -draw "color 1,1 point" -resize 20x20 -write mpr:grad ) -fill mpr:grad -strokewidth 5 -draw "text 20,80  GRADSTROKE" -delete 1 gradstroke.png
There are probably more elegant ways, but basically, just make a small image with a few pixels, then use -resize to smear it out. I wonder if there is an inline version of this, a trick posted elsewhere by Glenn Randers-Pehrson:

Code: Select all

echo "P3 2 2 255 0 220 240   0 140 150   0 140 150   0 80 80" | convert -geometry 512x512 - gradient.ppm
Let us know how it goes.

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

Re: Text with gradient stroke?

Post by el_supremo »

Try this. It creates the text as a separate image and then overlays it on a larger one with its position specified by -geometry.

Code: Select all

convert -size 1024x768 xc:white ( -size 600x100  xc:white -font Arial -pointsize 80 -fill gradient:#FFFF00-#ff0000 -strokewidth 5 -draw "text 20,80 GRADSTROKE" ) -geometry +400+260 -composite gradstroke1.png
Pete
froesi

Re: Text with gradient stroke?

Post by froesi »

Thank you guys, the past days I tried alot. I got it to work on the windows version (6.4.1-3).

Code: Select all

convert -size 1024x768 xc:white -font Helvetica -pointsize 80 (-size 20x80 gradient:#FFFF00-#ff0000 -write mpr:grad  -fill mpr:grad) -strokewidth 5 -draw "text 20,80  'GRADSTROKE'" -delete 1 wallpaper_test.jpg
I also have it on OpenSuse version (6.4.0.4), but here it'll not work. It says that the brackets are not allowed and so on.
Does anybody have an idea?

Thank you very much.

cheers
froesi
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Text with gradient stroke?

Post by rmabry »

froesi wrote:It says that the brackets are not allowed and so on.
Does anybody have an idea?
You probably just need to escape them:

\( blah blah \)

Rick
Post Reply