Python to Generate QR Code with Source Code

In this article, you’ll learn how to generate QR Code using python.

Prerequisites:

  1. Python Basics
  2. QR Code Basics
  3. pyqrcode module

What is QR Code?

  • Introduction QR is short for Quick Response.
  • A QR code is a type of matrix barcode first designed in 1994 for the automotive industry in Japan.
  • A barcode is a machine-readable optical label that contains information about the item to which it is attached.

QR Code vs Barcode

  • While QR Codes and Barcodes are similar in practice, QR Codes contain more information because they have the ability to hold information both horizontally and vertically.
  • Barcodes only use horizontal information.

Types of QR Code:

Static QR Code:

A Static QR Code contains information that is fixed and uneditable once the Code has been generated.

Usecases:

  • QR Codes in business cards or product packaging
  • QR Codes for personal use like a party invitation
  • QR Codes for Gyms

Dynamic QR Code:

Dynamic QR Codes allow you to update, edit and modify the type of the QR Code however many times you need i.e. the content is editable.

Usecases:

  • QR Codes for Coupons
  • QR Codes for Social media

Install Necessary Modules:

Open your Anaconda Prompt and type and run the following command (individually):

pip install pyqrcode
pip install pypng
  • pyqrcode the module is used to create QR Codes.
  • It is designed to be as simple and as possible.
  • It does this by using sane defaults and autodetection to make creating a QR Code very simple.

Once Installed now we can import it inside our python code.

Source Code:

'''
Python Program to Generate QR Code
'''

# Import the necessary module!
import pyqrcode

# Define the data:
# We need some text that we want to convert as our QR Code. 
# Since I am creating the QR Code for my github profile, 
# I will "https://programsolve.com/" as data here. Let's store inside a variable.
data = "https://programsolve.com/"

# Create qrcode:
# Now that we have the data with us, we can move forward and 
# make use of the package we just imported. Let's create an variable.
# We will use the create method (as it results in a cleaner looking code) on data.
qr = pyqrcode.create(data)

# Save the qrcode in png format with proper scaling:
# Now let's store it in .png format with proper scaling.
qr.png("programsolve.png", scale= 5)Code language: PHP (php)

Output:

Leave a Comment