How to convert SVG to PDF and PNG [Python]

Introduction

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. ..

Execution environment / Requirement

Execution method / Usage

1. Install svglib

First, run pip install svglib to install svglib.

2. Creating a Python file

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

3. Command execution

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! shell_result.png

Finally

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

How to convert SVG to PDF and PNG [Python]
Convert PDF to image (JPEG / PNG) with Python
Convert svg file to png / ico with Python
How to convert Youtube to mp3 and download it super-safely [Python]
[Python] How to convert db file to csv
How to package and distribute Python scripts
How to install and use pandas_datareader [Python]
How to convert Python to an exe file
Convert psd file to png in Python
python: How to use locals () and globals ()
Convert PDF files to PNG files with GIMP
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
Convert HEIC files to PNG files with Python
How to use is and == in Python
Convert DICOM to PNG with Ascending and Descending
How to install Python
How to generate permutations in Python and C ++
[Python] Convert from DICOM to PNG or CSV
How to install python
[Python] How to use hash function and tuple.
How to plot autocorrelation and partial autocorrelation in python
Python datetime How to calculate dates and convert strings strftime, strptime [Definitive Edition]
How to convert Web pages to PDF, PNG, JPG with VBA (Excel) (Selenium Basic)
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
[Python] [Django] How to use ChoiceField and how to add options
[Python] Convert general-purpose container and class to each other
[Python] Convert PDF text to CSV page by page (2/24 postscript)
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
How to convert JSON file to CSV file with Python Pandas
[Python] How to sort dict in list and instance in list
How to add page numbers to PDF files (in Python)
[Python] How to split and modularize files (simple, example)
[Python] How to create Correlation Matrix and Heat Map
python, php, ruby How to convert decimal numbers to n-ary numbers
Python # How to check type and type for super beginners
[2020.8 latest] How to install Python
[python] Convert date to string
Convert numpy int64 to python int
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Convert python 3.x code to python 2.x