ImageMagick - Text into rectangle

前端 未结 1 661
一向
一向 2020-12-15 13:58

I\'m very new to ImageMagick - I\'ve read some documentation and now I came to first real world situation. I have image (300*500) and I have some text which need to be fitte

相关标签:
1条回答
  • 2020-12-15 14:41

    Since you didn't provide a sample image for testing and applying some text to, I created one with the following command:

    convert                               \
       http://i.stack.imgur.com/RfJG6.png \
      -crop 312x513+579+0 +repage         \
       so#12231624-right.png
    

    Using the resulting image as an input, run these three commands to see how it would work (on Linux or Mac OS X):

    width=$(identify -format %W so#12231624-right.png)
    
    convert                  \
      -background '#0008'    \
      -gravity center        \
      -fill white            \
      -size ${width}x100     \
       caption:"This is a sample text to test \
    the automatic sizing of fonts by ImageMagick." \
       so#12231624-right.png \
     +swap                   \
     -gravity north          \
     -composite              \
      output1.png
    
    convert                  \
      -background '#0008'    \
      -gravity center        \
      -fill white            \
      -size ${width}x100     \
       caption:"This is a even longer sample text. \
    It also serves to test if automatic sizing of fonts \
    by ImageMagick works as expected: just don't specify \
    any fontsize, and let ImageMagick go for the best fit..." \
       so#12231624-right.png \
     +swap                   \
     -gravity north          \
     -composite              \
      output2.png
    

    Resulting images:

    output example 1 output example 2

    (The output doesn't match exactly your given frame -- but that's just because my test file still has a white, unfilled border (as part of the image) which I didn't bother to remove...)

    In other words: just do not bother to specify any font size using -fontsize. Only give the size of the region which should have the text annotation. Then ImageMagick will automatically pick the best matching font size and use it.

    0 讨论(0)
提交回复
热议问题