5 Best Ways to Generate QR Codes Using the PyQRCode Module in Python

πŸ’‘ Problem Formulation: You need to create QR codes within your Python application for various data payloads, such as URLs, text snippets, or other information. The objective is to generate a QR code that can be scanned to reveal the encoded data. The PyQRCode module in Python offers an accessible approach to generate these codes efficiently.

Method 1: Generating a Basic QR Code

The first method involves generating a basic QR code using the PyQRCode module. This involves creating a new QR code object from a string of data and then exporting it to an SVG or PNG file. The module automatically determines the optimal QR code size and encoding.

Here’s an example:

import pyqrcode

url = pyqrcode.create('https://www.example.com')
url.svg('example-qr.svg', scale=8)

The output is a QR code saved as an SVG file named ‘example-qr.svg’.

This code snippet first creates a new QR code object for a URL and then saves it as an SVG file. The scale parameter specifies the size of the QR code. A higher scale results in a larger QR code.

Method 2: Customizing QR Code Color and Background

The PyQRCode module allows for customization of the QR code’s foreground and background colors. This is useful for matching QR codes to your brand’s visual identity or simply to make the QR code stand out.

Here’s an example:

import pyqrcode
from pyqrcode import QRCode

qr = QRCode('https://www.example.com')
qr.png('example-qr.png', scale=6, module_color=[0, 0, 0, 128], background=[0xff, 0xff, 0xcc])

The output is a QR code with a semi-transparent black color and a light-yellow background.

This code creates a QR code with customized colors. The module_color option sets the color of the QR code itself, and background sets the background color. Colors are defined using RGBA for module_color and RGB for the background.

Method 3: Specifying Error Correction Level

QR codes have different levels of error correction to allow for recovery if parts of the code are damaged. The PyQRCode module supports setting the error correction level, which can enhance the robustness of the generated QR codes.

Here’s an example:

import pyqrcode

url = pyqrcode.create('https://www.example.com', error='H')
url.png('example-qr-high-error.png', scale=8)

The output is a QR code with high error correction capability.

The error parameter is used to set the error correction level. The levels range from ‘L’ (7% of data can be restored), ‘M’ (15% of data), ‘Q’ (25% of data), to ‘H’ (30% of data).

Method 4: Generating QR Code with Binary Data

Instead of textual or URL information, you might need to encode binary data into a QR code. PyQRCode allows for binary data input as a means to encode non-textual information or to specifically control the data format.

Here’s an example:

import pyqrcode

data = b'Example with binary data'
binary_qr = pyqrcode.create(data)
binary_qr.png('binary-data-qr.png', scale=8)

The output is a QR code that encodes binary data.

This snippet demonstrates how to generate a QR code from binary data. The binary data is passed directly to the create function. The resulting QR code is then saved as a PNG file.

Bonus One-Liner Method 5: Generating a QR Code in One Line

For quick and straightforward QR code generation, you can use a one-liner in Python with the PyQRCode module. This is suited for simple use-cases where you just need to create a code with default settings.

Here’s an example:

import pyqrcode; pyqrcode.create('https://www.example.com').png('quick-qr.png', scale=5)

The output is a standard QR code representing the given URL.

This one-liner creates and saves a QR code in a single command. It’s a condensed form of the basic generation process, perfect for lightweight applications or scripting.


  • Method 1: Basic QR Code Generation. Simple and efficient for generating QR codes without needing custom settings. However, it lacks customization options.
  • Method 2: Customizing QR Code Color and Background. Offers visual customization for branded applications. May require additional considerations for color contrast to ensure scannability.
  • Method 3: Specifying Error Correction Level. Enhances the durability of QR codes. Higher error correction results in denser codes, which require a larger space.
  • Method 4: Generating QR Code with Binary Data. Good for encoding non-textual data. Might not be as straightforward to use when working with text data.
  • Method 5: Bonus One-Liner. Fast and concise method for creating QR codes quickly. Offers no customization and uses default settings.