Multi-Color / Multi-Font Text

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
extol

Multi-Color / Multi-Font Text

Post by extol »

I would like to add multi-colored captions.

My text might be as follows on one picture ...
Rocky Mountains: Taken 10/12/2007

... and this way on the next one.
Dead Sea: Taken 11/11/2008

My problem is that, since my initial, red text, is of varying lengths, any scheme using compositing one image with one text color on top of a 2nd image with a 2nd text color is defeated by the fact that the x-position of the second text color text is determined by the width of the first text, which varies.

I hoped to just append them together, changing style information as I went ...

Any ideas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multi-Color / Multi-Font Text

Post by fmw42 »

extol wrote:I would like to add multi-colored captions.

My text might be as follows on one picture ...
Rocky Mountains: Taken 10/12/2007

... and this way on the next one.
Dead Sea: Taken 11/11/2008

My problem is that, since my initial, red text, is of varying lengths, any scheme using compositing one image with one text color on top of a 2nd image with a 2nd text color is defeated by the fact that the x-position of the second text color text is determined by the width of the first text, which varies.

I hoped to just append them together, changing style information as I went ...

Any ideas?
This seems to work. See http://www.imagemagick.org/Usage/text/

str1="Rocky Mountains"
str2=": "
str3="Taken 10/22/2007"
convert -background white \
\( -fill red -font Helvetica -pointsize 12 label:"$str1" \) \
\( -fill black -font Helvetica -pointsize 12 label:"$str2" \) \
\( -fill blue -font Helvetica -pointsize 12 label:"$str3" \) \
+append sometext.png

you can also set up string variables for the colors and the fonts

This is unix. If you are on windows, see http://www.imagemagick.org/Usage/api/#windows
extol

Re: Multi-Color / Multi-Font Text

Post by extol »

fmw42 wrote: This seems to work. See http://www.imagemagick.org/Usage/text/

str1="Rocky Mountains"
str2=": "
str3="Taken 10/22/2007"
convert -background white \
\( -fill red -font Helvetica -pointsize 12 label:"$str1" \) \
\( -fill black -font Helvetica -pointsize 12 label:"$str2" \) \
\( -fill blue -font Helvetica -pointsize 12 label:"$str3" \) \
+append sometext.png
Thanks. However, in my effort to simplify my question, I neglected to give you all the requirements.

Let's go on to say that my text is not guaranteed to fit on a single line and still remain within the confines of my text-area. Currently I'm using the caption: command, with a -size 320x to force line-wraps. This works perfectly, but, as I said, I'd like to change colors and/or fonts in mid-caption.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multi-Color / Multi-Font Text

Post by fmw42 »

Sorry, I don't think you can easily have that degree of flexibility with IM. But perhaps Anthony or someone else can suggest something further.
Last edited by fmw42 on 2009-02-02T20:59:22-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Multi-Color / Multi-Font Text

Post by anthony »

If the font family and font size does not change simple appending will work fine.

But if the fonts and sizes change then you have to somehow align the 'baseline' of the text.

see IM Examples, Text handling, Creating Lines of Mixed Font Styles
http://www.imagemagick.org/Usage/text/#mixed_font_lines

for my examples of aligning wildly different image.

the alturnative is to extract font information for each string so you can calculate the next position of each bit of text. That is also a lot easier than it use to be!!!!

See from the same page, Determining Font metrics
http://www.imagemagick.org/Usage/text/#font_info

When you know how far the 'caret' moved for each font, then you can generate a much more accurate composite of different fonts.



Final suggestion (may not be practical). If you continue to work with the same font, Why not just annotate the whole string. then recolor sections of the image based on extracted or (or saved) string lengths.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply