Text to caption/pango with several font options

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
perl
Posts: 3
Joined: 2016-10-17T14:07:03-07:00
Authentication code: 1151

Text to caption/pango with several font options

Post by perl »

Hi,

I'm trying to make an image of several words (hundreds), which may be on a file (separated by spaces)

For that purpose I'm using this command (which works very well)

convert -background lightblue -fill blue -font justbeautifulsimplicity -gravity center -size 4500x3000 caption:@/tmp/list_of_words.txt out.png

But now I want to alternate some font properties for the words. For example, alternate foreground color like this:

Firstword(fgcolor=red) Secondword(fgcolor=blue) Thirdword(fgcolor=red) Fourthword(fgcolor=blue) and so on and so forth.

I tried to use pango format like this:

convert -background lightblue -fill blue -font justbeautifulsimplicity -gravity center -size 4500x3000 pango:@/tmp/somepangofile.txt out.png

and doig some <span foreground="red"> on the file.

So the color changing is working now, but the problem here is that the text is not justified and the font size is not selected according to the size of the image.

Is there a way to accomplish this?

Thanks in advance,
Best regards.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Text to caption/pango with several font options

Post by snibgo »

Use "-define pango:justify=true" for justifying both ends of the line. See http://www.imagemagick.org/Usage/text/#pango

Pango responds to "-gravity", though rather strangely. See my "Pango" page. It doesn't have a "auto font size" facility.
snibgo's IM pages: im.snibgo.com
perl
Posts: 3
Joined: 2016-10-17T14:07:03-07:00
Authentication code: 1151

Re: Text to caption/pango with several font options

Post by perl »

Thank you!

Definitely an improvement, but still It does not accomplish the same thing as caption.

Now the problem is that the font size is not automatically calculated for the given image size. Imagemagick with pango seems to choose a very small font size. With "caption", the font size was choosen automatically to fit image resolution and fullfill the image.

Is it possible to get the same with a pango file?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Text to caption/pango with several font options

Post by anthony »

No. Pango is a completely different system. Just a library that IM passes the request to.
You will need to specify the resolution and pointsize you want.

However. You can use caption, and then get IM to report the pointsize it chose for the current resoltion. IM generally saves discovered information in per-image properities, so that users can then make use of it later. Once you have the pointsize you can then map that into pango! It may not come out exactly the same (they aren't the same system) but it should be close.

Examples of getting the pointsize IM selected is shown in
IM Examples, Set and Per-Image Properties
http://www.imagemagick.org/Usage/basics/#set

The example is for label but it should be present for caption too.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
perl
Posts: 3
Joined: 2016-10-17T14:07:03-07:00
Authentication code: 1151

Re: Text to caption/pango with several font options

Post by perl »

That did the trick. As you said, It is not exact, but It is very close and indeed a good starting point.

Thank you so much!
Post Reply