Python Generate QR Code as SVG file

This Python qrcode tutorial we show you how to creates a QR code image as a standalone SVG document using the API provided by qrcode image generator library.

Table of contents

  1. Install qrcode package
  2. How to generate QR code as SVG file in Python
  3. Other image factories to create QR code as SVG document

Install qrcode package

Installing the QR code generator package using the following command which will include pillow package for generating image.

pip install qrcode[pil]

How to generate QR code as SVG file in Python

The following Python program to show you how to generate a QR code as SVG file to encode given data ‘https://toricode.com/'

import qrcode
import qrcode.image.svg

data = 'https://toricode.com/'

qr_image = qrcode.make(data, image_factory=qrcode.image.svg.SvgImage)

qr_image.save('qrcode1.svg')

Execute the program above it will generate QR code as an SVF file named qrcode1.svg.

Other image factories to create QR code as SVG document

The qrcode package also provide different image factory you can use for image_factory parameter as following list.

  • qrcode.image.svg.SvgImage to creates SVG document with set of rects.
  • qrcode.image.svg.SvgFragmentImage to creates SVG document with set of rects.
  • qrcode.image.svg.SvgPathImage to create SVG which fixes white space that may occur when zooming.

Happy Coding 😊