使用Python的自定义字体将SVG转换为PNG

发布于 2025-01-19 09:28:35 字数 1051 浏览 2 评论 0原文

我已经阅读并尝试了几个线程,但无法解决这个问题。

我有一个简单的 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆流年 2025-01-26 09:28:35
from cairosvg import svg2png

from cairosvg import svg2png

svg_code = """
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <circle cx="12" cy="12" r="10"/>
        <line x1="12" y1="8" x2="12" y2="12"/>
        <line x1="12" y1="16" x2="12" y2="16"/>
    </svg>
"""

svg2png(bytestring=svg_code,write_to='output.png')

这个例子是由一个名为“JWL”的用户给出的,下面的链接是否回答了您的问题?

在 Python 中将 SVG 转换为 PNG

from cairosvg import svg2png

from cairosvg import svg2png

svg_code = """
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <circle cx="12" cy="12" r="10"/>
        <line x1="12" y1="8" x2="12" y2="12"/>
        <line x1="12" y1="16" x2="12" y2="16"/>
    </svg>
"""

svg2png(bytestring=svg_code,write_to='output.png')

this example was given by a user called "JWL", does the below link answer your question?

Convert SVG to PNG in Python

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文