Label interword-spacing fit, but multiple spaces in 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
queensoft
Posts: 14
Joined: 2014-02-14T09:12:48-07:00
Authentication code: 6789

Label interword-spacing fit, but multiple spaces in text

Post by queensoft »

I need to fit (justify) a text to the bottom of an image.
I use label interword-spacing fit, and it's great when there's just one single space in the text: "www.google.com (555)123-4567"
But, now I have multiple spaces: "www.google.com CALL (555) 123-4567"
I need the final text to be stretched like this:
-www.google.com-on the left side
- big empty gap in the middle
- CALL (555) 123-4567 - on the right side, one single space between words
Like this: http://www.google.com __________________ CALL (555) 123-4567
_ is space
My command line is this, the text is spread over the entire bottom evenly:

Code: Select all

www.google.com _____ CALL _____ (555) _____ 123-4567

Code: Select all

convert.exe -size 824x450 xc:none -font Arial -size 824x -fill #0d0707 -stroke rgb(255,255,255) -background transparent -gravity South -interword-spacing 100  label:"www.google.com CALL (555) 123-4567" -bordercolor transparent -border 15x15 -composite +repage -depth 8 output.png
Thanks.

Later edit: looks like I found a workaround:
1. save command output to a text line. Use underscores instead of spaces, so there's one single space in the text line.

Code: Select all

convert.exe -size 824x450 xc:none -font Arial -size 824x -fill #0d0707 -stroke rgb(255,255,255) -background transparent -gravity South -interword-spacing 100 -debug annotate  label:"www.google.com CALL_(555)_123-4567" -bordercolor transparent -border 15x15 -composite +repage -depth 8  null: 2> x.txt
Last line will contain the pointsize of the resulting text:

Code: Select all

 Font c:\windows\fonts\arial.ttf; font-encoding none; text-encoding none; pointsize 32.25
2. Extract pointsize using AutoIt, I'm pretty sure you can extract this using any simple tool/programming language.
3. Create 2 labels, SouthWest and SouthEast, use spaces this time:

Code: Select all

[code]convert.exe -size 824x450 xc:none -font Arial -fill #0d0707 -stroke rgb(255,255,255) -background transparent -gravity SouthWest -pointsize 32.25 label:"www.google.com" -compose over -composite -font Arial -fill #0d0707 -stroke rgb(255,255,255) -background transparent -gravity SouthEast -pointsize 32.25 label:"CALL (555) 123-4567" -bordercolor transparent -border 15x15 -composite +repage -depth 8  output.png
[/code]
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Label interword-spacing fit, but multiple spaces in text

Post by GeeMack »

queensoft wrote:I need to fit (justify) a text to the bottom of an image.
It seems like "-annotate" might be a better tool to get what you want done. This seems to be pretty close...

Code: Select all

convert.exe -size 824x450 xc:none -font Arial -pointsize 32.25 -fill #0d0707 -stroke rgb(255,255,255) -background transparent -gravity southwest -annotate +0+0 "www.google.com" -gravity southeast -annotate +0+0 "CALL (555) 123-4567" -bordercolor transparent -border 15x15 +repage -depth 8 output.png
That places the text directly on your background canvas without any compositing.

Also, please provide the version of IM you're using and which OS you're working on.
queensoft
Posts: 14
Joined: 2014-02-14T09:12:48-07:00
Authentication code: 6789

Re: Label interword-spacing fit, but multiple spaces in text

Post by queensoft »

Where did you get that pointsize value of 32.25 ?
I forgot to mention: I batch process many images.
Every time there's a different random font, I have about 800 fonts.
So, each font will have a different final pointsize.

I just put my workaround together and it seems to work perfectly.

I use Windows 7 Ultimate x64 SP1 and ImageMagick-7.0.1-Q16, 64 bits
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Label interword-spacing fit, but multiple spaces in text

Post by GeeMack »

queensoft wrote:Where did you get that pointsize value of 32.25 ?
You found the pointsize. I didn't.
I forgot to mention: I batch process many images.
Every time there's a different random font, I have about 800 fonts.
So, each font will have a different final pointsize.
The "-interword-spacing" operator is based on number of pixels, not point size. Also, the width of an underscore will not necessarily be the same as the width of a space, so your spacing is somewhat arbitrary any way you're doing it. If you just want to make sure there's 100 pixels between the first text group and the second, there are still ways to manage that without building and compositing multiple labels.
I just put my workaround together and it seems to work perfectly.
There are many approaches to get various tasks done with ImageMagick. If you're satisfied with how a particular way works, it is the right way for you.
I use Windows 7 Ultimate x64 SP1 and ImageMagick-7.0.1-Q16, 64 bits
The IM7 command will be "magick" not "convert".
queensoft
Posts: 14
Joined: 2014-02-14T09:12:48-07:00
Authentication code: 6789

Re: Label interword-spacing fit, but multiple spaces in text

Post by queensoft »

I was able to extract the desired pointsize and it worked.

Yes, underscore may be different size than space. But I'm not that picky, I do not need 1000% exact perfect match.
I just ran my program about 80 times and the result was perfect each time.
In my case, debug result matched the label pointsize perfectly.
I need text to be justified both sides left and right:
- left:www.google.com
- right: CALL (555) 123-4567
Using different fonts means that the middle gap is always different size.
Come to think of if, 3rd time I'm writing/editing this, it also seems weird to me, there's no good way to justify this text the way I want it.
I mean, how big do you make that middle gap ?
But, anyways, the workaround works perfectly :)

I was wondering why IM7 does not have the convert.exe anymore !!!
So I just downloaded the portable version and this one had the convert.exe
I also did a quick search (really quick) for IM7 changelog / script breaking changes / missing convert.exe.... but found nothing useful.
I will use magick.exe in the future.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Label interword-spacing fit, but multiple spaces in text

Post by snibgo »

I assume you installed on Windows.
At installation, did you tick the option for:

Code: Select all

"Install legacy utilities (e.g. convert)"
snibgo's IM pages: im.snibgo.com
Post Reply