How can I redraw a text without "-draw" to get only outward 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?".
Post Reply
Kree
Posts: 7
Joined: 2015-04-17T12:04:41-07:00
Authentication code: 6789

How can I redraw a text without "-draw" to get only outward stroke?

Post by Kree »

I try to create a text image with ImageMagick, where is the stroke expands only outward. I found a solution, where I should use the "-draw" command, but with it I would need to define the size of my image, but I don't know it in advance.

The command below should be upgrated. Somehow I would need to draw the text again on it, without strokewidth:

Code: Select all

convert -background none -font Tahoma -fill white -pointsize 100 -stroke red -strokewidth 20 label:Test text stroke.png
I asked this question on Stackoverflow too, and there was a solution, what didn't work for me, because I got this finally:
Image
The plan is good, but the positioning isn't enought exact, the second text shifted to right from the expected position.
Maybe it would be a good point to solve my problem, but I stucked with it...

Any help would be appreciated!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I redraw a text without "-draw" to get only outward stroke?

Post by fmw42 »

try this

Unix:

Code: Select all

convert -respect-parentheses \
\( -background none -font Tahoma -fill red -pointsize 100 -stroke red -strokewidth 20 label:"Test text" \) 
-gravity center -font Tahoma -fill white -pointsize 100 -annotate -20-10 "Test text" result.png


Windows:

Code: Select all

convert -respect-parentheses ^
( -background none -font Tahoma -fill red -pointsize 100 -stroke red -strokewidth 20 label:"Test text" ) 
-gravity center -font Tahoma -fill white -pointsize 100 -annotate -20-10 "Test text" result.png

Next time please identify your version of IM and platform!
Kree
Posts: 7
Joined: 2015-04-17T12:04:41-07:00
Authentication code: 6789

Re: How can I redraw a text without "-draw" to get only outward stroke?

Post by Kree »

fmw42 wrote:try this
Windows:

Code: Select all

convert -respect-parentheses ^
( -background none -font Tahoma -fill red -pointsize 100 -stroke red -strokewidth 20 label:"Test text" ) 
-gravity center -font Tahoma -fill white -pointsize 100 -annotate -20-10 "Test text" result.png
Next time please identify your version of IM and platform!
Hello fmw42! Sorry for the lately answer, and for the missing parameters. My platform is Windows, and the IM version is 6.7.7.

How can you decide / calculate the value of the -annotate? This is the critical part of my problem.

Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I redraw a text without "-draw" to get only outward stroke?

Post by fmw42 »

I am not sure. Since you had a strokewidth of 20, I had expected to offset it by -10-10, but found that it needed -20-10. I suspect that it has to do with label: autofit in relation to gravity center used for annotate.

My best suggestion is to use label: to get the size of the output. Then start over and use annotate twice to do the rendering.

In unix, I tried this:

Code: Select all

size=`convert -background none -font Tahoma -fill red -pointsize 100 -stroke red -strokewidth 20 label:"Test text" -format "%wx%h" info:`
echo $size

convert -size $size xc:none \
-gravity center -font Tahoma -fill red -pointsize 100 -stroke red -strokewidth 20 -annotate +0+0 "Test text"  \
-gravity center -font Tahoma -fill white -pointsize 100 -stroke white -strokewidth 0 -annotate +0+0 "Test text" result.png
The vertical centering is correct, but the horizontal centering is off by the strokewidth and needs to be brought back by - half the strokewidth. I think the stroke is offset horizontally and this may be a bug.

So this seems to work:

Code: Select all

strokewidth=20
offset=`convert xc: -format "%[fx:$strokewidth/2]" info:`
echo $offset
size=`convert -background none -font Tahoma -fill red -pointsize 100 \
-stroke red -strokewidth $strokewidth label:"Test text" -format "%wx%h" info:`
echo $size

convert -size $size xc:none \
-gravity center -font Tahoma -fill red -pointsize 100 -stroke red -strokewidth $strokewidth -annotate +0+0 "Test text"  \
-gravity center -font Tahoma -fill white -pointsize 100 -stroke white -strokewidth 0 -annotate -${offset}+0 "Test text" result.png
A windows person will need to tell you how to set up variables and use them in the command line.
Post Reply