Home » Generate QR codes using Python

Generate QR codes using Python

In this article we look at a way of generating QR codes using Python

QR code  is a type of  two-dimensional barcode invented in 1994 by the Japanese automotive company Denso Wave.

A barcode is a machine-readable optical label that can contain information about the item to which it is attached. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application.

A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently; extensions may also be used.

The Quick Response system became popular outside the automotive industry due to its fast readability and greater storage capacity compared to standard UPC barcodes. Applications include product tracking, item identification, time tracking, document management, and general marketing.

A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera, and processed using Reed–Solomon error correction until the image can be appropriately interpreted.

The required data is then extracted from patterns that are present in both horizontal and vertical components of the image

Installation

For a standard install (which will include pillow for generating images), run:

pip install qrcode[pil]

Examples

A basic example

In this example we create a qr code

# importing the qrcode library  
import qrcode  
# generating a QR code using the make() function  
qr_img = qrcode.make("Welcome to maxpython.")  
# saving the image file  
qr_img.save("qr-img.jpg")  

This created the following QR code

 

Generating an Advanced QR Code

The programmers can customize a QR code using a QRCode object which consists of the parameters shown in the following table:

Parameter Description
version There are forty (40) versions of the QR code, which regulates the size of the code. Version 1 is the smallest, whereas version 40 is the largest. Version 1 generates a 21×21 matrix QR code.
error_correction This parameter is used to control the Error Correction utilized for the QR code.

  1. ERROR_CORRECT_L: up to 7%
  2. ERROR_CORRECT_M: up to 15%
  3. ERROR_CORRECT_Q: up to 25%
  4. ERROR_CORRECT_H: up to 30%
box_size This parameter is used to regulate the number of pixels in the individual block of the QR code.
border This parameter is used to control the thickness of the border. The default border is 4 pixels thick.
import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('Welcome to maxpython.')
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="yellow")

Link

https://github.com/lincolnloop/python-qrcode

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More