如何在 Imagemagick 中为文本添加填充?

How to add padding to text in Imagemagick?

我有这张 500 像素宽的图片。想要在左侧和右侧的文本中添加一些白色 space 或填充。 我试过了:

convert source.jpg \
        -size 500x \
        -background blue \
        -fill white \
        -pointsize 32 \
        caption:"Sed felis eros, ornare ut cursus a, imperdiet sit amet purus." \
        +swap \
        -append \
        output.jpg

结果:

然后尝试使用拼接,它添加了一个蓝色列:

convert source.jpg \
        -size 500x \
        -background blue \
        -fill white \
        -pointsize 32 \
        caption:"Sed felis eros, ornare ut cursus a, imperdiet sit amet purus." \
        -splice 10x10 \
        +swap \
        -append \
        output.jpg

结果:

如何只在蓝色文本框周围填充内边距?

也许-extent with a -gravity选项?下面的示例(用颜色来说明)

convert \( \
       -size 500x \
       -background blue \
       -fill white \
       -pointsize 32 \
       caption:"Sed felis eros, ornare ut cursus a, imperdiet sit amet purus." \
   \) \
   source.jpg \
   -append \
   -background green \
   -gravity Center \
   -extent 520x \
   output.jpg

或者只是文本...

convert \( \
        -size 480x \
        -background blue \
        -fill white \
        -pointsize 32 \
        caption:"Sed felis eros, ornare ut cursus a, imperdiet sit amet purus." \
        -gravity Center \
        -extent 500x \
    \) \
    source.jpg \
    -append \
    output.jpg

根据评论编辑

要包含 top/bottom & left/right padding,那么你不妨我们 -border

 convert -size 480x \
         -background blue \
         -fill white \
         -pointsize 32 \
         caption:"Sed feels eros, ornate ut cursus a, imperdiet sit amet purus." \
         -bordercolor green \
         -border 10x20 \
         output.png

这比您想象的要容易。使用这样的东西:

convert source.jpg \
        -size 500x \
        -background blue \
        -fill white \
        -pointsize 32 \
        -bordercolor blue  -border 10
        caption:"Sed felis eros, ornare ut cursus a, imperdiet sit amet purus." \
        +swap \
        -append \
        output.jpg

这是通过添加与背景颜色相同的边框来实现的,因此它看起来像是添加了内边距,但实际上这添加了一个具有相同颜色值 10px 的边框。所以,使用它,它完美地工作。