How to create a QR Code image or SVG in Python

A Quick Response (QR) code is a bi-dimensional pictographic code that is used due to its fast readability and relatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. If you are working with Python and you need to create quickly a QR Code, we'll show you how you can achieve this in a couple of seconds using the qrcode library.

1. Install required libraries

The first library that you need to add in Python is Pillow. The Python Imaging Library, widely known as PIL as well or Pillow in newer versions, (in newer versions known as Pillow) is a free library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. To create the QR Code we are going to use the qrcode library that relies on pillow

You can install this module executing the following command in your terminal:

REM Required to work with images
pip install Pillow

Once the installation of Pillow finishes, you can proceed with the installation of the QR generator library:

REM install library to generate QR Codes
pip install qrcode

Otherwise, without Pillow you get the message error 'ImportError: No module named Image' during the creation of an image with the QR Code library. For more information about Pillow or qrcode, please visit their homepages.

2. Creating QR Code as an image

The code that you need to write to create a QR Code is very simple to understand and pretty straightforward. First import the qrcode library and use the QRCode method from it, then provide the arguments as needed

# Import QR Code library
import qrcode

# Create qr code instance
qr = qrcode.QRCode(
    version = 1,
    error_correction = qrcode.constants.ERROR_CORRECT_H,
    box_size = 10,
    border = 4,
)

# The data that you want to store
data = "The Data that you need to store in the QR Code"

# Add data
qr.add_data(data)
qr.make(fit=True)

# Create an image from the QR Code instance
img = qr.make_image()

# Save it somewhere, change the extension as needed:
# img.save("image.png")
# img.save("image.bmp")
# img.save("image.jpeg")
img.save("image.jpg")

Error correction

A QR Code has error correction capability to restore data if the code is damaged or dirty. Four error correction levels are available with this library, they're stored in the qrcode.constants object:

  • ERROR_CORRECT_L: About 7% or less errors can be corrected.
  • ERROR_CORRECT_M: (default) About 15% or less errors can be corrected.
  • ERROR_CORRECT_Q: About 25% or less errors can be corrected.
  • ERROR_CORRECT_H: About 30% or less errors can be corrected.

They should be providen as value of the error_correction property during the creation of the QR Code.

QR Code size

You can change the size of the generated QR code with the box_size property.

3. Creating QRCode as SVG

If you are willing to generate an SVG file of the QRCode instead of an image, then you will need to install the lxml library as well, because the older xml.etree.ElementTree version can not be used to create SVG images. lxml is a XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API.

To install this module using pip, run the following command in the terminal:

pip install lxml

After the installation, you will be able to generate an ElementTree for the SVG file that will be generated. The generation method of the SVG can be different according to your needs, the library offers 3 type of SVG namely a SVG Image, using fragments or paths:

import qrcode
import qrcode.image.svg

# define a method to choose which factory metho to use
# possible values 'basic' 'fragment' 'path'
method = "basic"

data = "Some text that you want to store in the qrcode"

if method == 'basic':
    # Simple factory, just a set of rects.
    factory = qrcode.image.svg.SvgImage
elif method == 'fragment':
    # Fragment factory (also just a set of rects)
    factory = qrcode.image.svg.SvgFragmentImage
elif method == 'path':
    # Combined path factory, fixes white space that may occur when zooming
    factory = qrcode.image.svg.SvgPathImage

# Set data to qrcode
img = qrcode.make(data, image_factory = factory)

# Save svg file somewhere
img.save("qrcode.svg")

Happy coding !

Carlos Delgado

About the author

Carlos Delgado

Interested in programming since he was 14 years old, Carlos is the founder and author of most of the articles at Our Code World. He is currently studying systems engineering at the UDI university in Colombia.

This could interest you
Become a more social person