How to generate QR Code using Python..๐Ÿ

Python Tutorial Blog

ยท

3 min read

How to generate QR Code using Python..๐Ÿ

Hello People,

In this Blog article, we will learn how to create QR code with just few lines of Python code using qrcode library. It is a fun project to try and easy to understand.

Check out the Repository in python. Drop a star if you find it useful !!!

QR Code (Quick Response code), is a two-dimensional version of the vertical line stripe bar code that you see from retail packaging. QR Code provides quick access to access different type of information in the code

QR Code is pretty widely use in almost all industries these days, and to read QR Code, all you need to do is do download an app on your phone that reads QR code. Also, you can store literally any types of information inside a QR Code.

Python offers a QRCode package which makes it really easy to implement this functionality. We will begin by downloading the following python package.

  • pip install qrcode

Video Tutorial:

You can watch my YouTube video Tutorial to see a working tutorial for better understanding and a step by step Guide to write a code.

Code time:

# import qrcode library using "pip install qrcode"
import qrcode

# add parameter for QR code image.
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=2,
    border=8
)

# Information which we want to convert in QR code.
qr.add_data(input("Enter your text which you want to convert in qr code: "))

# Generate QR code.
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")

# save the image.
img.save("QR2.png")

print("QR code is ready.")

The QRCode function used above having following parameters:

version: This parameter defines the size of the QR Code generated. It accepts integer values in the range of 1 to 40. The higher this value, bigger the dimension of the generated QR Code image.

The error_correction parameter controls the error correction used for the QR Code. The following four constants are made available on the qrcode package:

ERROR_CORRECT_L: About 7% or less errors can be corrected. ERROR_CORRECT_M (default): About 15% or less errors can be corrected. ERROR_CORRECT_Q: About 25% or less errors can be corrected. ERROR_CORRECT_H: About 30% or less errors can be corrected.

box_size: This parameter defines the size of each box in pixels.

border: The border defines the thickness of the border. In this example, the value of 5 means that the border is equal to the thickness of 5 tiny black boxes.

The add_data method is used to pass our input text, which is the hyperlink to the article in our case. The make function 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.

The last step is to convert this into an image file and store it. For this, the make_image function is used where we can specify the foreground and background color.

After that i have save this QR code by using .save method.

Output:

qr1.png

See you in my next Blog article, Take care!!

Dhruvin Gajjar.

TheCreators

Resources:

ย