Open In App

Generate QR Code using qrcode in Python

A Quick Response Code or a QR Code is a two-dimensional bar code used for its fast readability and comparatively large storage capacity. It consists of black squares arranged in a square grid on a white background.

Python has a library “qrcode” for generating QR code images. It can be installed using pip.



pip install qrcode

Approach:

Syntax:



qrcode.make('Data to be encoded')

Example 1:




# Importing library
import qrcode
 
# Data to be encoded
data = 'QR Code using make() function'
 
# Encoding data using make() function
img = qrcode.make(data)
 
# Saving as an image file
img.save('MyQRCode1.png')

Output:

Example 2:

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

Below is the implementation:




# Importing library
import qrcode
 
# Data to encode
data = "GeeksforGeeks"
 
# Creating an instance of QRCode class
qr = qrcode.QRCode(version = 1,
                   box_size = 10,
                   border = 5)
 
# Adding data to the instance 'qr'
qr.add_data(data)
 
qr.make(fit = True)
img = qr.make_image(fill_color = 'red',
                    back_color = 'white')
 
img.save('MyQRCode2.png')

Output :


Article Tags :