Sometimes caption: produces text that's too small.

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
arizonagroovejet
Posts: 28
Joined: 2009-01-20T12:37:52-07:00

Sometimes caption: produces text that's too small.

Post by arizonagroovejet »

Using caption: to automatically fit some text in to a box of a given size mostly works fine but sometimes the text is way too small. For example,

Code: Select all

$ convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640x240 -interline-spacing 0 caption:"K FHYERAN, WOW, WHAT A TOUGH SENTENCE FOR PAUL MANAFORT, WHO HAS REPRESENTED RONALD REAGAN, BOB DOLE AND MANY OTHER TOP POLITICAL AAAAAA AAA AAAAAAAAA. AAAA'A AAAA AAAAAAAA AAA AAA AAAA AA." /tmp/blah.jpg
produces this
Image
which is fine.

Code: Select all

$ convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640x240 -interline-spacing 0 caption:"K FHYERAN, WOW, WHAT A TOUGH SENTENCE FOR PAUL MANAFORT, WHO HAS REPRESENTED RONALD REAGAN, BOB DOLE AND MANY OTHER TOP POLITICAL AAAAAA AAA AAAAAAAAA. AAAA'A AAAA AAAAAAAA AAA AAA AAAA AA. A" /tmp/blah.jpg
produces this
Image
and it seems to be that it shouldn't.

I'm using Fedora 28. I get the above described results using ImageMagick as installed from the Fedora 28 repos

Code: Select all

$ convert --version
Version: ImageMagick 6.9.9-38 Q16 x86_64 2018-03-12 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib
$ rpm -qf $(which convert)
ImageMagick-6.9.9.38-1.fc28.x86_64
and also using a version which I built from source.

Code: Select all

$ convert --version
Version: ImageMagick 7.0.8-1 Q16 x86_64 2018-06-18 https://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: https://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP 
Delegates (built-in): bzlib fontconfig freetype jng jpeg pangocairo png x zlib

The DejaVu-Sans-Bold font is provided by the package dejavu-sans-fonts-2.35-6.fc28.noarch.

I have also seen the problem with Noto_Sans-Bold font as provided by google-noto-sans-fonts-20161022-7.fc28.noarch but no longer have a record of a string which triggers the problem with that font.

It seems like the same behaviour described at viewtopic.php?t=28897 I thought about trying the examples in that post but lack of the arial font and lost patience with the process of getting it installed on Fedora, which involves jumping through various hoops which presumably circumvent issues relating to it's non-free status. I've not found any other discussions which describe anything that seems remotely like the problem I'm seeing. I have spent about an hour experimenting and been unable to figure out anything at all about the conditions which cause the text to be strangely small.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sometimes caption: produces text that's too small.

Post by fmw42 »

You specified neither -pointsize nor a width or height via -size. Try including one or the other.
arizonagroovejet
Posts: 28
Joined: 2009-01-20T12:37:52-07:00

Re: Sometimes caption: produces text that's too small.

Post by arizonagroovejet »

Both of my examples specify width and height via -size.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sometimes caption: produces text that's too small.

Post by fmw42 »

Sorry, I missed that. When your size just matches the characters you have provided it work well. But when you add one more character, it no longer fits the size and the font must be reduces so that it does not break a word.

I find it better to provide a width and pointsize, such as

Code: Select all

convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640 -pointsize 36 -interline-spacing 0 caption:"K FHYERAN, WOW, WHAT A TOUGH SENTENCE FOR PAUL MANAFORT, WHO HAS REPRESENTED RONALD REAGAN, BOB DOLE AND MANY OTHER TOP POLITICAL AAAAAA AAA AAAAAAAAA. AAAA'A AAAA AAAAAAAA AAA AAA AAAA AA. A" tmp1.jpg

Code: Select all

convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640 -pointsize 36 -interline-spacing 0 caption:"K FHYERAN, WOW, WHAT A TOUGH SENTENCE FOR PAUL MANAFORT, WHO HAS REPRESENTED RONALD REAGAN, BOB DOLE AND MANY OTHER TOP POLITICAL AAAAAA AAA AAAAAAAAA. AAAA'A AAAA AAAAAAAA AAA AAA AAAA AA." tmp2.jpg
arizonagroovejet
Posts: 28
Joined: 2009-01-20T12:37:52-07:00

Re: Sometimes caption: produces text that's too small.

Post by arizonagroovejet »

fmw42 wrote: 2018-06-19T09:18:53-07:00 When your size just matches the characters you have provided it work well. But when you add one more character, it no longer fits the size and the font must be reduces so that it does not break a word.
Sure, more text requires a small font size, but my point is that in some cases the extent to which the font size is reduced is wildly disproportionate. In the second example of my original post, the result does not look to me like "best fit". It looks to me like the font size could be a lot larger. That the text is sometimes so small feels like a bug to me, but I didn't post in Bugs forum because I don't know if maybe I'm missing something about how caption's best fit functionality works. Also I can provide very little information about how to trigger the problem.
fmw42 wrote: 2018-06-19T09:18:53-07:00I find it better to provide a width and pointsize
The behaviour I want is this:
As of IM v6.3.2, if you provide both the width and the height of the final image, but do not define the "-pointsize" of the font (or turn off the pointsize with "+pointsize"), IM will attempt to automatically adjust the size of the font so as to best fill the "-size" of the image you requested.
from http://www.imagemagick.org/Usage//text/#caption_bestfit
Specifying a pointsize value will not get me that behaviour.

This http://www.imagemagick.org/discourse-se ... hp?t=28557 seems like another example of the problem. Unfortunately the only reply seems somewhat miss the point by saying that the result from using one of the strings is OK, when that one string is the same one that the original poster said worked OK. I couldn't recreate the problem with the example text and font mentioned in that post using 6.9.9 or 7.0.8.

The images produced by this

Code: Select all

$ for i in {1..300};do j=0;s="";while [ $j -lt $i ];do s="${s} A" ;((j++));done; convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640x240 caption:"${s}" /tmp/blah_${i}.jpg ;done 
all seem reasonable to me, so the issue is more complicated than best fit goes weird over a certain character count.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sometimes caption: produces text that's too small.

Post by fmw42 »

Best fit must consider the font sizes available and not break words. More than than you will have to wait to hear back from one of the developers.
arizonagroovejet
Posts: 28
Joined: 2009-01-20T12:37:52-07:00

Re: Sometimes caption: produces text that's too small.

Post by arizonagroovejet »

I know nothing about how TrueType fonts work. I found someone saying that that can contain both bitmap (glyphs at various fixed sizes) and outline which scale to any size. I coudln't find anything else that confirmed what that person had said or made me think it wasn't correct. If the font I'm using is only available in N fixed sized variants, that could account for the behaviour I'm seeing. But the images produced by

Code: Select all

$ for i in {1..100};do convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640 -pointsize $i caption:"hello world" ps_${i}.jpg;done
shows that the font scales to any of those sizes.

I've found more examples of where caption picks a font size which I think is way too small. E.g.

Code: Select all

$ convert -background white -font DejaVu-Sans-Bold -fill white -stroke black -strokewidth 2 -size 1640x240 'caption:Tartaglia hemadromograph hairworm aerodermectasia kinkily unrein propolis unbolted Bosnian calo- padshah antipass harling nixer decalcomania CSRG luxuriates colt becoward unrounding non-Swedish concluded unglamorous undertint mutules materiels autocolony forcibly bringer-up shortliffe sphaeroblast librarians bielenite evenlong TSE disingenuous stolidity lacquerer phonogrammically reloaned mortifiedly unrequickened fy Lotson defectuous alcoholometry angusti- twanger Rexer nonjuridically nonbranded policyholder rottennesses basque Calvert dauke penology ureteroproctostomy Maitund hairstone consonantal woilie Pseudobranchus Isabel sloan unpillaged ovicapsular preinterview sandy-bearded analogon repacification CRC Alderney levanting balled geonegative necessity Ramaite eremiteship Maia detrench revuette belleter contrastedly unvitreosity Cully ventrolaterally tends doggerel unplunge eatery denseness vicargeneral dumb-show malacopodous acichloride Gabrilowitsch self-government Sassetta OODB ' /tmp/blah.jpg

Code: Select all

$ convert -background white -font DejaVu-Sans-Bold -fill white roke black -strokewidth 2 -size 1640x240 'caption:spalls ignominies exoneration golden probe pyromucate round-rolling clavicymbal biopyribole drop-kick misaccused ferryboats flockiest unwanton genepi flame-colour rockfishes sassybark aggregation overmarking vigilantes wrath-bewildered all-governing neurosyphilis Itasca achromatinic phytoserologically prooflike Mandell chippy undependableness Boetius shriekily truthfulness terrorproof gravilea Ndebeles swire submotive Maryalice furfuran militarizes tonal unfeverish A.U.C. wheedle dilutent reacceding unpalliable motor-mindedness epidemiography coween acetylbenzene bookstores logarithmic spino- discouple idioreflex discourage myeloencephalitis borel unbefittingly intelligibility hapax MPV galleon Lib-Lab clinician dustoffs trait greenhide upcurved unworked subantique McCullers you-be-damned Templetonia precentral gaes cardiolith foreigneering homo Flossi overbooked hideousnesses nonvenally swimmers punctualness directorial ungodliest Landsman vindication burstone carcajous Utrillo fackins Saxonic lambie Rosemont hieroglyphy ' /tmp/blah.jpg
I found the above examples by writing a script which makes an image using 100 random words.

Code: Select all

$ cat foo.sh 
#!/bin/bash
set -x
for i in {1..100};do convert -background white -font DejaVu-Sans-Bold  -fill white -stroke black -strokewidth 2 -size 1640x240 caption:"$(shuf -n 100 /usr/share/dict/words| tr '\n' ' ')" /tmp/blah_${i}.jpg ;done 
Invoke that as

Code: Select all

$ ./foo.sh 2>&1 | tee fooout.txt
Then look through the resulting images. I found that in any given run most images were fine, but there were always some which weren't. The command that created any given image can be found be searching fooout.txt.
Post Reply