ImageMagick允许您将标题应用于图像。标题文本会自动调整大小并自动换行,以适合您定义的区域。
通过命令行使用ImageMagick,我可以像这样定义这个标题的笔划宽度和颜色:
convert -size 300x300 -stroke black -strokewidth 1 -fill white \
-background transparent -gravity center \
caption:"This is a test of the caption feature in ImageMagick" ~/out.png

我在网上找不到如何使用MagickWand C绑定来应用这些属性。我可以创建标题并更改其字体和字体颜色,但我不知道如何添加笔划。
我想知道这些信息,以便在Python的Wand绑定中添加对此的支持。我愿意采用另一种方法来实现自动调整文本大小的重力和笔划,但最好不需要不雅的变通方法或外部软件。
作为进一步的信息,我在通过自制软件安装的macOS 10.13.6上使用ImageMagick 6.9.10-10。
发布于 2018-08-27 23:07:14
从技术上讲,你将负责构建一个绘图上下文,并计算单词换行。通常通过调用MagickQueryMultilineFontMetrics。
但是,caption:协议是作为捷径提供的。您可以使用review the source code查看如何实现此类计算,但如果您不感兴趣,可以在调用Image Read方法之前使用MagickSetOption快速破解解决方案。
使用C
#include <wand/MagickWand.h>
int main(int argc, const char * argv[]) {
MagickWandGenesis();
MagickWand * wand;
wand = NewMagickWand();
// -size 300x300
MagickSetSize(wand, 300, 300);
// -stroke black
MagickSetOption(wand, "stroke", "BLACK");
// -strokewidth 1
MagickSetOption(wand, "strokewidth", "1");
// -fill white
MagickSetOption(wand, "fill", "WHITE");
// -background transparent
MagickSetOption(wand, "background", "TRANSPARENT");
// -gravity center
MagickSetGravity(wand, CenterGravity);
// caption:"This is a test of the caption feature in ImageMagick"
MagickReadImage(wand, "caption:This is a test of the caption feature in ImageMagick");
// ~/out.png
MagickWriteImage(wand, "~/out.png");
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}使用wand
from wand.image import Image
from wand.api import library
with Image() as img:
# -size 300x300
library.MagickSetSize(img.wand, 300, 300)
# -stroke black
library.MagickSetOption(img.wand, b"stroke", b"BLACK")
# -strokewidth 1
library.MagickSetOption(img.wand, b"strokewidth", b"1")
# -fill white
library.MagickSetOption(img.wand, b"fill", b"WHITE")
# -background transparent
library.MagickSetOption(img.wand, b"background", b"TRANSPARENT")
# -gravity center
img.gravity = "center"
# caption:"This is a test of the caption feature in ImageMagick"
img.read(filename="caption:This is a test of the caption feature in ImageMagick")
# ~/out.png
img.save(filename="~/out.png")https://stackoverflow.com/questions/52030585
复制相似问题