如何生成其中包含数字的图像?
How to generate an image with a number in it?
ImageMagic 的这个命令创建了一个图像,在其中心写入变量 var
的值:
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -draw 'text 0,0 '"$var"' ' "$var".png
虽然它适用于字符串,但当 var
的值为数字时它会失败:
convert-im6.q16: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3265.
由于 bash 中的变量是无类型的,因此无法将其转换为字符串(命令 printf
将保持原样,一个数字),所以关键是要改变命令中的 text
参数。
变量为数值(正数、负数、实数)时如何生成图像?
虽然 the documentation for -draw
的 text
运算符仅表示要呈现的字符串如果有空格则需要引用,但似乎也需要引用如果它是数字。所以...
-draw "text 0,0 '$var' "
您还可以使用 -annotate 代替 -draw 在 Imagemagick 中的图像中放置数字。
var=7
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -annotate +0+0 "$var" "$var".png
Or
var=7
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -annotate +0+0 $var $var.png
但使用双引号最安全
ImageMagic 的这个命令创建了一个图像,在其中心写入变量 var
的值:
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -draw 'text 0,0 '"$var"' ' "$var".png
虽然它适用于字符串,但当 var
的值为数字时它会失败:
convert-im6.q16: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3265.
由于 bash 中的变量是无类型的,因此无法将其转换为字符串(命令 printf
将保持原样,一个数字),所以关键是要改变命令中的 text
参数。
变量为数值(正数、负数、实数)时如何生成图像?
虽然 the documentation for -draw
的 text
运算符仅表示要呈现的字符串如果有空格则需要引用,但似乎也需要引用如果它是数字。所以...
-draw "text 0,0 '$var' "
您还可以使用 -annotate 代替 -draw 在 Imagemagick 中的图像中放置数字。
var=7
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -annotate +0+0 "$var" "$var".png
Or
var=7
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -annotate +0+0 $var $var.png
但使用双引号最安全