Multiline text image using -draw

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
sderrick
Posts: 4
Joined: 2017-03-23T10:30:52-07:00
Authentication code: 1151

Multiline text image using -draw

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiline text image using -draw

Post 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, ...
snibgo's IM pages: im.snibgo.com
sderrick
Posts: 4
Joined: 2017-03-23T10:30:52-07:00
Authentication code: 1151

Re: Multiline text image using -draw

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiline text image using -draw

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiline text image using -draw

Post 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
snibgo's IM pages: im.snibgo.com
sderrick
Posts: 4
Joined: 2017-03-23T10:30:52-07:00
Authentication code: 1151

Re: Multiline text image using -draw

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiline text image using -draw

Post 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
sderrick
Posts: 4
Joined: 2017-03-23T10:30:52-07:00
Authentication code: 1151

SOLVED: Re: Multiline text image using -draw

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiline text image using -draw

Post by fmw42 »

Sorry, I also overlooked 0,0 rather than +0+0
Post Reply