You can set a size that specifies how large a space is available for your text and ImageMagick will choose the largest pointsize text that will fit:
magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc caption:"Short Text" short.png
magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc caption:"Somewhat longer text that will get rendered in a smaller font" long.png
If you want a margin around the text, you can set a maximum size for the text, then increase the size of the canvas with -extent
- I'll do that in red so you can see what the -extent
added:
magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc caption:"Somewhat longer text" -background red -extent 410x400 long.png
If you are reading lines from files to generate your hundreds of thousands of images, you can pipe the text in from another command like this:
echo -n "Text supplied by other command" | magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc caption:@- result.png
If you want to know what pointsize ImageMagick chose, you can get it like this:
magick identify -format "%[caption:pointsize]\n" result.png
59