Writing text on top of a background image

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
Blackeye
Posts: 2
Joined: 2011-09-15T06:02:38-07:00
Authentication code: 8675308

Writing text on top of a background image

Post 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",
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Writing text on top of a background image

Post 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
Blackeye
Posts: 2
Joined: 2011-09-15T06:02:38-07:00
Authentication code: 8675308

Re: Writing text on top of a background image

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

Re: Writing text on top of a background image

Post 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
Post Reply