Page 1 of 1

Multiline text image using -draw

Posted: 2017-03-23T10:58:58-07:00
by sderrick
Linux, ImageMagick 6.5.4-7 2012-05-07 Q16 OpenMP

Trying to watermark images and allow multiline text to be used. The user can define there own string. I'm building an image of the text and then compositing it over the image to be water marked. The text image is multi shaded so it is visible on any background.

I'm using the -draw text primitive to build the text image(stamp.png). I initially used -annotate which allows me to use the \n newline for multiline but the results were not as good as the -draw text allows. My text is stored in the variable STEXT.

How can I pass in a newline character to -draw text?

Code: Select all

convert -size $markSize xc:grey30 -font $FONT -pointsize $POINT -gravity center \
          -draw "fill grey  text 0,0  '$STEXT'" \
          stamp_fgnd.png

convert -size $markSize xc:black -font $FONT -pointsize $POINT -gravity center \
          -draw "fill grey  text  1,1  '$STEXT'  \
                             text  0,0  '$STEXT'  \
                 fill black  text -1,-1 '$STEXT'" \
          +matte stamp_mask.png
          
composite -compose CopyOpacity  stamp_mask.png  stamp_fgnd.png  stamp.png
mogrify -trim +repage stamp.png
composite -gravity $WHERE -geometry +0+10 stamp.png  $FILE $FILE.jpg
thanks, scott

Re: Multiline text image using -draw

Posted: 2017-03-23T11:32:47-07:00
by snibgo
sderrick wrote:How can I pass in a newline character to -draw text?
I don't think you can. See http://www.imagemagick.org/Usage/text/#draw :
As of IM version 6.2.4, the "-draw text" operation no longer understands the use of '\n' as meaning newline, ...

Re: Multiline text image using -draw

Posted: 2017-03-23T11:37:51-07:00
by sderrick
It seems odd that there is no way to do multiline text with -draw? No back door or work around?

Is there a different primitive like -draw that will allow me to place the text and specify the fill color. I understand that annotate is a wrapper around -draw to provide a simpler interface, yet it allows the use of a newline char. Must be some way to make draw work directly?

Re: Multiline text image using -draw

Posted: 2017-03-23T11:38:55-07:00
by fmw42
I believe it still can be used in -annotate or caption: or label:

Annotate is not any harder to use than -draw and allows the same or more flexibility with drawing text. It allows slanting or rotating the text and you can use -geometry for the offsets.

Re: Multiline text image using -draw

Posted: 2017-03-23T11:53:34-07:00
by snibgo
Perhaps you can do it by putting a newline character into $TEXT. That would be a single character, not the character pair \n.

You can do it via @file.txt or @-.

For example, make hw.txt containing:

Code: Select all

text 50,50 'hello
world'
The use a command such as:

Code: Select all

convert -size 300x200 xc: -draw "@hw.txt" out.png

Re: Multiline text image using -draw

Posted: 2017-03-23T12:00:26-07:00
by sderrick
thanks, I tried that, no joy.

I have found that replacing

Code: Select all

convert -size $markSize xc:grey30 -font $FONT -pointsize $POINT -gravity center \
          -draw "fill grey  text 0,0  '$STEXT'" \
          stamp_fgnd.png
with

Code: Select all

convert -size $markSize xc:grey30 -font $FONT -pointsize $POINT -gravity center \
          -fill grey  -annotate 0,0  "$STEXT" \
          stamp_fgnd.png
provides the newline, but the text is barely readable..

getting close

thanks for all teh input.

scott

Re: Multiline text image using -draw

Posted: 2017-03-23T13:31:32-07:00
by fmw42
What is your IM version and platform? There should be no difference if font size if the same pointsize is used? What is your version of freetype. See convert -list format and it should say for TTF, such as

TTF* TTF r-- TrueType font (Freetype 2.7.1)

Using IM 6.9.8.2 Q16, both of these give the same size font and provide the new line. Note that the text in -draw has a line feed provided by the return key right after the first "testing"

Code: Select all

convert -size 200x200 xc:white -pointsize 24 -fill black -gravity center \
-draw "text 0,0 'testing
testing'" tmp1.png

Code: Select all

convert -size 200x200 xc:white -pointsize 24 -fill black -gravity center \
-annotate +0+0 "testing\ntesting" tmp2.png

SOLVED: Re: Multiline text image using -draw

Posted: 2017-03-23T14:40:13-07:00
by sderrick
fmw42,
thanks you for the clue I needed. :D
I had

Code: Select all

-annotate 0,0 "testing\ntesting" tmp2.png
instead of

Code: Select all

-annotate +0+0 "testing\ntesting" tmp2.png
soon as I swapped 0,0 with +0+0 all was well!

thanks all for the help and direction. sorry if I'm a bit slow on the uptake, I so rarely need to the any more than basic IM tasks.

Scott

Re: Multiline text image using -draw

Posted: 2017-03-23T15:37:54-07:00
by fmw42
Sorry, I also overlooked 0,0 rather than +0+0