I wanted to convert SVG to PNG, so I tried CairoSVG
, but I found that svglib
is easier to convert, so I will introduce the method. ..
First, run pip install svglib
to install svglib.
Next, let's create a Python file like the one below.
svg2png.png
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM
import sys
import os
args = sys.argv
filename = args[1]
filename_without_ext = os.path.splitext(os.path.basename(filename))[0]
drawing = svg2rlg(filename)
renderPDF.drawToFile(drawing, filename_without_ext + ".pdf")
drawing = svg2rlg(filename)
renderPM.drawToFile(drawing, filename_without_ext + ".png ", fmt="PNG")
Open Terminal, command prompt, etc., specify the SVG file as shown below, and execute it.
python
python svg2png.py sample.svg
If the PNG and PDF files are generated as shown below, the conversion is successful!
How was that? I hope you can convert it well.
I also created a repository on Github, so please use this if you like m (_ _) m https://github.com/Masumi-M/svg2png
Recommended Posts