Fitting text to image [SOLVED]

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
Brabantis
Posts: 5
Joined: 2013-03-10T17:52:28-07:00
Authentication code: 6789

Fitting text to image [SOLVED]

Post by Brabantis »

Hello,
I am using ImageMagick on a Mac, and I wanted to use it to create a geeklet (for GeekTool) that would display info slightly rotated clockwise. I have a code that would fetch some text, then make a rotated image of it. The code I am using for simple texts is:

Code: Select all

{part of code that fetches the text}| /opt/local/bin/convert -rotate 15 -background none -fill black -pointsize 24 -font Courier label:@- -trim /tmp/label.png
It works fine, but I wanted to make it possible, for lengthier texts, to set the width of the image (before the rotation) and have the text adjusted to it. Until now I only managed to crop it.
Please ask for explanation if I was not clear, I am not a native English speaker.
Thank you!
Last edited by Brabantis on 2013-03-11T02:42:14-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fitting text to image

Post by fmw42 »

You need to create the text image before rotation. To get a specific width (before rotation), you need to use -size and remove the pointsize.

So something like the following should work (but in the current version, it is not expanding the text to fill the width). See viewtopic.php?f=3&t=22946


convert -size 200x -background none -fill black -font arial label:"some text" -rotate 15 output
Brabantis
Posts: 5
Joined: 2013-03-10T17:52:28-07:00
Authentication code: 6789

Re: Fitting text to image

Post by Brabantis »

Didn't work. From the little I know of coding, your method is for adjusting the size of the letters to fill the width; I just wanted the text to go to the next line whenever it would go over a certain width. Like in Notepad.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fitting text to image

Post by snibgo »

(Notepad is a Windows text editor that word-wraps. It needs to know the font size and page margins, of course.)

"label:" can't word-wrap. "caption:" can. See http://www.imagemagick.org/Usage/text/#caption

As fmw42 says, do the rotation after the "caption:".
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: Fitting text to image

Post by fmw42 »

Brabantis wrote:Didn't work. From the little I know of coding, your method is for adjusting the size of the letters to fill the width; I just wanted the text to go to the next line whenever it would go over a certain width. Like in Notepad.

Sorry that was not clear in your earlier post. So (as snibgo said) use caption in place of label

convert -size 200x -background none -fill black -font arial caption:"some text" -rotate 15 output

Note that the rotation will make your resulting image bigger than the 200 width. I assume you understand that.
Brabantis
Posts: 5
Joined: 2013-03-10T17:52:28-07:00
Authentication code: 6789

Re: Fitting text to image

Post by Brabantis »

That didn't work either. There re a couple things I noticed though. First, as far as my limited skill goes I cannot use "label" or "caption" because the text is not fixed, but the result of the previous script. Also "rotate" didn't work if not in the first position, and it did not seem to work with "size".
My problem is that I am writing a widget that displays an RSS feed on my desktop, and I wanted it to fit on a rotated square of parchment; id wouldn't fit if I don't wrap the text.

EDIT: Ok, I found a solution by tweaking the first script. Thanks anyway :D
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fitting text to image [SOLVED]

Post by snibgo »

What version IM are you running? If "-rotate" needs to be first, it must be a very old version. If so, I suggest you upgrade.
snibgo's IM pages: im.snibgo.com
Brabantis
Posts: 5
Joined: 2013-03-10T17:52:28-07:00
Authentication code: 6789

Re: Fitting text to image [SOLVED]

Post by Brabantis »

I upgraded last week :shock:
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Fitting text to image [SOLVED]

Post by anthony »

If the text is not fixed, you need to generate a caption for each of the text strings, then rotate.

There is no other way to get rotated and word wrapped text, caption (and pango) are the only word wrapping text to image converters in IM.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Brabantis
Posts: 5
Joined: 2013-03-10T17:52:28-07:00
Authentication code: 6789

Re: Fitting text to image [SOLVED]

Post by Brabantis »

Yes, but I solved by altering the other code, the one that generates RSS feeds
Post Reply