hw to offset text from the top of an image and give it a text alignment of center

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

hw to offset text from the top of an image and give it a text alignment of center just like the image in the link explain what am trying to do
https://drive.google.com/open?id=0Bzloe ... 2JwSW8ybkU
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by snibgo »

I don't understand what you want. "-annotate +X+Y hello" will offset the string by +X+Y relative to the gravity setting. With "-gravity North", this is relative to the top-centre of the image.
snibgo's IM pages: im.snibgo.com
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

i will want an offset from the top of the image not relative to the gravity setting, however i may want to use the gravity settings for my text alignment meaning if i want a text to be align left, i will use the gravity settings of west am trying to acheive something like these :
https://drive.google.com/open?id=0Bzloe ... 2JwSW8ybkU
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

somebody should please help me, pls.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

Try this. Create each line of text using label:. Then append them using -smush to put the 20 pixel spacing. Then composite that over a white background at your desired location. Adjust the border spacing and color and text color and font and point size as desired.

Code: Select all

convert -background white -fill black -gravity center -pointsize 36 label:"my first text" -trim +repage tmp1.png
convert -background white -fill black -gravity center -pointsize 36 label:"center text" -trim +repage tmp2.png
convert tmp1.png tmp2.png -gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 tmp3.png
convert -size 500x500 xc:white tmp3.png -geometry +57+146 -compose over -composite text_result.png
Image

In one command line in unix format it would be:

Code: Select all

convert \
\( -background white -fill black -gravity center -pointsize 36 label:"my first text" -trim +repage \) \
\( -background white -fill black -gravity center -pointsize 36 label:"center text" -trim +repage \) \
-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 \
\( -size 500x500 xc:white \) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

@fmw, u're using the label image operator, could t be possible to use the annotate IO.i really appreciate ur suggest above.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

You can use annotate, but you need to do one annotate for each line of text and provide the exact location where you want the text to be located. You can chain the two annotates in the same command line. But -annotate does not draw boxes. So you would have to add a -draw "rectangle x1,y,1 x2,y2" to add that. My label command seems the easier way to go so that I did not have to located each line of text separately and draw the box separately.
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

putting bracket around command, what does it help u to achieve? and pls is there any book that could be recommend as to teach me, hw to write imagemagick command. Am using IM for a project and its taking me time to learn and understand the flow of the script.
could u pls show me an example IM command of ur last comment.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

Using parentheses, allows you to do multiple separate commands in the same command line without the settings affecting subsequent parts in other parentheses. See http://www.imagemagick.org/Usage/basics/#parenthesis

Here is the command written with annotate and draw, but I had to tweak it several times to get it aligned properly. The advantage of the label method is that it aligns automatically.

Code: Select all

convert -size 500x500 xc:white \
-fill black -pointsize 36 -font arial -gravity northwest \
-annotate +78+167 "my first text" \
-annotate +85+213 "center text" \
-fill none -stroke red -strokewidth 10 \
-draw "rectangle 65,155 275,263" -alpha off \
text_result2.png
Image

See
http://www.imagemagick.org/Usage/text/#annotate
http://www.imagemagick.org/Usage/draw/#primitives

All the books are very old. See http://www.imagemagick.org/script/sitemap.php#help

But the best resources are:

viewtopic.php?f=1&t=9620http://http://w ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

sorri am replying late @fm pls i tried to test the code and has not been working. here is wat have tried:

Code: Select all

<?php
$ou = "magick "; 
$ou .= "(-background white -fill black -gravity center -pointsize 36 label:'my first text' -trim +repage ) ";
$ou .= "(-background white -fill black -gravity center -pointsize 36 label:'center text' -trim +repage ) ";
$ou .= "-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "(-size 500x500 xc:white ) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
exec("$ou");
?>
its not working. can u pls help me fix it.
thank u very much i so much appreciate your time
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by snibgo »

You need a space after each "(".

I suggest you capture and report errors.
snibgo's IM pages: im.snibgo.com
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

@snibgo , pls hw do i capture and report errors. can u give me the snippet to report error.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by snibgo »

I don't use PHP. There are examples on these forums.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 »

try this

Code: Select all

<?php
$ou = "magick "; 
$ou .= "(-background white -fill black -gravity center -pointsize 36 label:'my first text' -trim +repage ) ";
$ou .= "(-background white -fill black -gravity center -pointsize 36 label:'center text' -trim +repage ) ";
$ou .= "-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "(-size 500x500 xc:white ) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
exec("$ou 2>&1", $out, $returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
FIXED to remove the extra quote in the exec line
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 »

snibgo wrote: 2017-08-25T15:56:08-07:00 I don't use PHP. There are examples on these forums.
can you please point to one of them
Post Reply