使用Python的自定义字体将SVG转换为PNG
我已经阅读并尝试了几个线程,但无法解决这个问题。
我有一个简单的 SVG 文件:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 289 144.94">
<defs>
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Cabin:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700');
</style>
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Cabin:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700');
</style>
<style type="text/css">.cls-11{font-size:53.58px;}
</style>
</defs>
<text class="cls-11" textLength="110" lengthAdjust="spacingAndGlyphs" transform="translate(160.98 45.54)">M2/1</text>
</svg>
但我无法在 Python 中为此 SVG 生成 PNG 输出。我尝试过 cairosvg、pyvips、wandpy,但都遇到了一些问题。 主要是2个问题:
- 导入的字体未加载/无法识别。
- 大多数人似乎都忽略了 lenghtAdjust 参数。
我也可以接受从中生成另一种基于像素的格式或使用另一种编程语言/库。
I have read and tried several threads, but I was unable to solve this problem.
I have a simple SVG file:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 289 144.94">
<defs>
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Cabin:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700');
</style>
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Cabin:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700');
</style>
<style type="text/css">.cls-11{font-size:53.58px;}
</style>
</defs>
<text class="cls-11" textLength="110" lengthAdjust="spacingAndGlyphs" transform="translate(160.98 45.54)">M2/1</text>
</svg>
But I'm unable to generate a PNG output for this SVG in Python. I have tried cairosvg, pyvips, wandpy, but all had some sort of issues.
Mainly the 2 problem:
- The imported font is not loaded/not recognized.
- The lenghtAdjust parameter seems to be ignored by most of them.
It would be also acceptable for me to generate another pixel-based format from this or use another programming language/library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个例子是由一个名为“JWL”的用户给出的,下面的链接是否回答了您的问题?
在 Python 中将 SVG 转换为 PNG
this example was given by a user called "JWL", does the below link answer your question?
Convert SVG to PNG in Python