【问题标题】:Convert SVG image to PNG image by python通过python将SVG图像转换为PNG图像
【发布时间】:2022-07-05 15:57:23
【问题描述】:

我在这段代码中使用了 svglib:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM

drawing = svg2rlg('''E:/img/1926_S1_style_1_0_0.svg''')
renderPM.drawToFile(drawing, 'image.jpg', fmt='jpg')

但是我收到 它来自 SVG 的图像,例如 那么我应该怎么做才能以正确的方式将 SVG 转换为 PNG?

【问题讨论】:

    标签: python svg


    【解决方案1】:

    尝试使用cairosvg

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

    answer by JWL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 2018-11-19
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2017-12-21
      相关资源
      最近更新 更多