使用默认字体作为标题
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论