3 months

Generating QR Codes and Barcodes in Python

QR code (quick response code) is a type of matrix barcode (or two-dimensional barcode) invented in 1994 by the Japanese automotive company Denso Wave. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application. QR codes use 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 the horizontal and vertical components of the image. 

Barcode is basically an encoded image that’s machine-readable and the machine just reads the number that is below the barcode and it also reads the parallel lines like the spacing between the parallel lines as shown in this picture. It is mostly used in product identification. Barcode actually encodes visual patterns line, for example, the spacing between the lines or the number, and that’s how it gets the information. 

 The Python-qrcode module is a pure Python library for generating QR codes. It can be used in both simple and complex QR code applications. It offers methods to encode arbitrary sequences of data into QR codes as well as a way to decode QR codes. 


 It can be installed using pip.

pip install qrcode                                                   

import qrcode                                                              

qrcode = qrcode.make('the Joy of Choice')                                                                            


We can also use QRCode class to create a QR Code and change its details. It takes the following parameters:

  • Version: This parameter is an integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21×21 matrix).
  • error_correction:  This parameter controls the error correction used for the QR Code. There are following four constants available for this :
    • qrcode.constants.ERROR_CORRECT_L About 7% or fewer errors can be corrected.
    • qrcode.constants.ERROR_CORRECT_M (default) : About 15% or fewer errors can be corrected.
    • qrcode.constants.ERROR_CORRECT_Q: About 25% or fewer errors can be corrected.
    • qrcode.constants.ERROR_CORRECT_H: About 30% or fewer errors can be corrected.
  • box_size: This parameter controls how many pixels each “box” of the QR code is.
  • border: The border parameter controls how many boxes thick the border should be (the default is 4, which is the minimum in the specification).
  • add_data(): This method is used to add data to the QRCode object. It takes the data to be encoded as a parameter.
  • make(): This method with (fit=True) ensures that the entire dimension of the QR Code is utilized, even if our input data could fit into less number of boxes.
  • make_image(): This method is used to convert the QRCode object into an image file. It takes the fill_color and back_color optional parameters to set the foreground and background color.

import qrcode                                                                                                           

data = "athaenas"                                                                                                         

qr = qrcode.QRCode(version = 7,box_size = 17, border = 7)                                      

qr.make(fit = True)                                                                                                         
img = qr.make_image(fill_color = 'blue',back_color = 'white')                                        

import qrcode                                                                                             
qr = qrcode.QRCode(version = 2,box_size = 15,border = 10)                   

data = 'https://www.google.co.in/'                                                                 
img = qr.make_image(fill = 'black', back_color = 'white')                              


How to generate Barcode:                                                                                

python-barcode: It is a Python module that creates barcodes, including Code 128 and Code 39. It’s just the bare minimum needed to get an SVG image of a barcode without having to manually draw out paths and characters yourself. It also provides us with different standard types of barcodes such as EAN-8, EAN-13, EAN-14, UPC-A, JAN, ISBN-10, ISBN-13, and many more. Run the following command in your terminal/command prompt to install it:

pip install python-barcode

pillow: It is also a required module, it will help us create the barcode in image formats.  Run the following command in your terminal/command prompt to install it:

 pip install pillow                                                                                            

from barcode import EAN13                                                                   

number = '71741098761417'                                                                  
code = EAN13(number)                                                                          


This  is currently outputting in SVG format. To get the output in PNG format, we can make the following change to our code:

from barcode import EAN13                                                                              

from barcode.writer import ImageWriter                                                            
number = '17178734127417'                                                                            
code = EAN13(number, writer=ImageWriter())                                                  

They can store up to 4,296 characters in the code format. They are most often used to store URLs.  QR codes can be generated using the python-qrcode library. The library includes functions for generating QR codes based on URL requests, drawings, input boxes. If you need to use the QR code facility of your Python program, consider installing this library. We have used two modules, python-barcode for generating QR code barcodes with the code formatting and the qrcode which can be used to create QR codes visually with controllable parameters.