gpt4 book ai didi

python - 将描边应用于标题 : using MagickWand

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:37 26 4
gpt4 key购买 nike

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

This image is the output from the command, for reference.

我在网上找不到任何地方如何使用 MagickWand C 绑定(bind)来应用这些属性。我可以创建标题并更改其字体和字体颜色,但我不知道如何添加笔画。

我想知道这些信息,以便将对此的支持添加到 Python 的 Wand 绑定(bind)中。我愿意接受另一种方法来使用引力和笔划完成自动调整大小的文本,但最好不需要不雅的解决方法或外部软件。

作为进一步的信息,我在通过 Homebrew 安装的 macOS 10.13.6 上使用 ImageMagick 6.9.10-10。

最佳答案

从技术上讲,您将负责构建绘图上下文并计算自动换行。通常通过调用 MagickQueryMultilineFontMetrics .

但是,caption: 协议(protocol)是作为快捷方式提供的。你可以review the source code查看如何实现此类计算,但如果您不感兴趣,可以在调用图像读取方法之前使用 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;
}

使用魔杖

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")

关于python - 将描边应用于标题 : using MagickWand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com