Home > Back-end >  Convert SVG image to PNG image by python
Convert SVG image to PNG image by python

Time:07-05

I have use svglib with this code :

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

But what i receive What i convert It's from the image of SVG like enter image description here So what should i do to convert SVG to PNG in the right way?

CodePudding user response:

try using 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

  • Related