Page 1 of 1

Writing text on top of a background image

Posted: 2011-09-16T00:23:55-07:00
by Blackeye
Hi,

I'm working due to my new job with Imagemagick for a few days and have a question concering writing text on top of a background image.

I do this in the following perl script (without background image, only background-color):

Code: Select all

system "convert",

    "-size", "468x60",
    "-fill", "#8F000F",
    "-draw", "color 0,0 reset",
    "-fill", "white",
    "xc:transparent", # in newer ImageMagick Versions instead use: "canvas:none",
    "-pointsize", "12", 
    @draw,

$filename;
This works fine, but I am not able to get the background image in it.

I would be glad if someone could give me a hint which command I have to use to place the background image in this script.

Thanks,

Blackeye

EDIT:

I now get two files. One is the background-image, and the other one is my generated text. Both are named with $filename and a counter at the end. This is when I save them as jpg and png. I wrote that this will hapen. When I save it as gif, I'll get an animated image sequence. But I just want the two images put together. How can I do this?

Here is what I added to my code:

Code: Select all

"-adjoin", "path/to/facebook.gif",

Re: Writing text on top of a background image

Posted: 2011-09-16T04:46:36-07:00
by Bonzo
I do not use perl but try this test and work from there ( I prefer annotate ):

Code: Select all

convert input_image -fill Black -pointsize 12 -gravity center  -annotate +0+0 "Some text" output_image

Re: Writing text on top of a background image

Posted: 2011-09-16T05:22:50-07:00
by Blackeye
Hej Bonzo!

Thanks for your draft, but I already made a little step forward. With this code I can get the text on top of the background image:

Code: Select all

system "convert",

    "-size", "600x148","xc:transparent", # in newer ImageMagick Versions instead use: "canvas:none",
    "-draw", "image over  0,0 0,0 '$home/tmp/facebook.gif'",
    @draw,
        
$filename;
In the @draw-Array is the following command:

Code: Select all

"-draw", "text 25,60 'textexttexttextetxte'",
Now I've got another problem. My text in the @draw is now too long and he doesn't make a line break (of course, I didn't say it to him :D ) ... but how can i prevent this. The text in the @draw comes from a database and so I am not able to shorten him.

Here is a pic of my problem: http://www.bilder-space.de/show_img.php ... e=original

Re: Writing text on top of a background image

Posted: 2011-09-16T10:02:26-07:00
by fmw42
You will need to do your text using caption: so that it wraps to the next line when reaching the end of your desired width. Draw it on a transparent background and then composite it on top of the background image. see http://www.imagemagick.org/Usage/text/#caption.

try this for example (assuming your are on Unix/Mac) if not see http://www.imagemagick.org/Usage/windows/ for syntax differences.



dim=`convert logo: -resize 50% -format "%w" info:`
convert \( logo: -resize 50% \) \
\( -size ${dim}x -background none -fill black -font Arial -pointsize 24 -gravity center \
caption:"This is a very long message that hopefully will be wider than the image" \) \
-gravity north -geometry +0+20 -compose over -composite logo_tmp1.png