使用 ImageMagick 在图像中心添加文本

问题描述 投票:0回答:1

我试图在现有图像的中心绘制文本,但无法达到预期的结果:

这是到目前为止我已经尝试过的

convert src-image.png -gravity center -pointsize 144 -fill red -annotate -50 'REGISTERED' temp2.png

我能够以一定角度在中心实现文本,但我的要求是为多个图像尺寸提供这样的文本,这些尺寸可能会有所不同。

  • 字体大小应足够大以便可见
  • 文字应从左下角开始 -> 右上角

Here是我尝试过但无法获得准确工作命令的参考之一。 请让我知道如何针对可变图像尺寸实现以下结果。 预期图像:enter image description here

imagemagick imagemagick-convert
1个回答
0
投票

我能够使用链接答案中的以下 shell 脚本参考来实现所需的结果:

#!/bin/bash

# Width, height and text
w=$1
h=$2
text=$3
source_file=$4
target_file=$5

# Get pointsize ImageMagick thinks is good
pointsize=$(convert -gravity center -size ${w}x${h} caption:"$text" -format "%[caption:pointsize]" info:)

echo ImageMagick likes pointsize: $pointsize

# So draw text in that size on larger canvas, trim to bounds of letters and resize to desired size
wb=$((w*2))
hb=$((h*2))
convert $source_file -gravity center -fill white -size ${wb}x${hb} -pointsize $pointsize -annotate -45 "$text" -trim +repage -resize ${w}x${h}\! $target_file

来电者

./paint.sh 5237 7852 "REGISTERED" input-image.jpeg result.jpg
© www.soinside.com 2019 - 2024. All rights reserved.