使用默认字体作为标题

发布于 2025-01-20 19:36:06 字数 1442 浏览 1 评论 0原文

我正在尝试使用 python3-wand 重新创建以下命令行调用:

$ convert -size 4330x -density 300x300 -background transparent -gravity center -fill grey label:"This is a long text" -trim -rotate -54 +repage -resize 1984x2806 -extent 2480x3508 /tmp/watermark.png

使用以下代码:

width_px = 2480
height_px = 3508
hypothenuse = int(math.sqrt(width_px**2 + height_px**2) * 0.8)
with Image(
    width=hypothenuse,
    height=hypothenuse,
    resolution=300,
    pseudo="xc:transparent",
) as img:
    img.gravity = "center"
    img.caption(
        args.watermark,
        font=Font(
            "/usr/share/fonts/opentype/urw-base35/NimbusSans-Regular.otf",
            color=Color("grey"),
        ),
    )
    img.trim()
    img.rotate(-math.degrees(math.atan(height_px / width_px)))
    img.transform(resize=f"{width_px * 0.8}x{height_px * 0.8}^")
    img.extent(
        width_px,
        height_px,
        int(-(width_px - img.width) / 2),
        int(-(height_px - img.height) / 2),
    )
    img.save(filename="/tmp/out.pdf")

除非我像上面那样硬编码 font 属性,否则我得到错误:

TypeError: font must be specified or existing in image

字体似乎是一个真正的野兽,我不想将字体硬编码到 代码,我也不想使用 TTFQuery 作为 文档建议,因为该项目似乎没有维护。

相反,我只想使用默认字体,例如命令行版本 做。这有可能吗?

I'm trying to use python3-wand to recreate the following command-line invocation:

$ convert -size 4330x -density 300x300 -background transparent -gravity center -fill grey label:"This is a long text" -trim -rotate -54 +repage -resize 1984x2806 -extent 2480x3508 /tmp/watermark.png

using the following code:

width_px = 2480
height_px = 3508
hypothenuse = int(math.sqrt(width_px**2 + height_px**2) * 0.8)
with Image(
    width=hypothenuse,
    height=hypothenuse,
    resolution=300,
    pseudo="xc:transparent",
) as img:
    img.gravity = "center"
    img.caption(
        args.watermark,
        font=Font(
            "/usr/share/fonts/opentype/urw-base35/NimbusSans-Regular.otf",
            color=Color("grey"),
        ),
    )
    img.trim()
    img.rotate(-math.degrees(math.atan(height_px / width_px)))
    img.transform(resize=f"{width_px * 0.8}x{height_px * 0.8}^")
    img.extent(
        width_px,
        height_px,
        int(-(width_px - img.width) / 2),
        int(-(height_px - img.height) / 2),
    )
    img.save(filename="/tmp/out.pdf")

Unless I hard-code the font attribute as above, I get an error:

TypeError: font must be specified or existing in image

Fonts seem to be a real beast, and I neither want to hardcode a font into the
code, nor doe I want to use TTFQuery as
suggested by the docs, because that project seems unmaintained.

Instead, I'd like to just use a default font, like the command-line version
does. Is that somehow possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文