How to Generate and Read a QR Code in Python?

Posted in /  

How to Generate and Read a QR Code in Python?
vinaykhatri

Vinay Khatri
Last updated on December 10, 2024

    In this Python tutorial, we will walk you through the Python program to generate and read a QR code. In the first program, we will generate a QR code and save it locally as a png image, and then in the second program, we will read the same image to extract the QR code data.

    What is QR Code?

    The QR code is a matrix barcode, which is a machine-readable optical code that stores data about the item attached to it. When you buy a product, you can see a bar code or QR code attached to it that stores the general data about the item.

    To read the barcode or QR code, we require a barcode reader that converts optical machine-readable code to human-readable text. But before we dive into the QR generation and reading programs, let's install the libraries that we will need to complete this Python tutorial.

    Installing Python Libraries to Generate and Read QR Code in Python

    1) Python qrcode Library

    qrcode is an open-source Python library that is used to generate the QR code using Python programs. We will be using this library to generate a QR code for some data and save it locally as a png image. You can install this library using the following pip install command:

    pip install qrcode

    2) Python Pillow Library

    Pillow is one of the most popular Python image processing libraries and using this library, we can handle different types of images in Python.

    In this tutorial, we will be using the Pillow library to load the generated QR code for decoding or reading its data. To install the Pillow library on your Python environment, run the following pip install command:

    pip install Pillow

    3) Python pyzbar Library

    pyzbar is also an open-source third-party Python library. It can read the images and extract all types of bar code. In this tutorial, we will be using this library to extract data from the QR code. To install the pyzbar library on your Python environment, run the following pip install command on the terminal or command prompt:

    pip install pyzbar

    How to Generate and Read a QR Code in Python?

    1. Generating a QR Code in Python

    Alright then, let's start with generating the QR code. First, import the required modules.

    import qrcode

    After importing the qr module, let's define the data for which we want to generate the QR code. For this tutorial, we will be generating a QR code for techgeekbuzz.com.

    data = "https://www.techgeekbuzz.com/"

    Now initialize an qrcode.QRCode() object qr with some arguments.

    #initialize qr object
    qr = qrcode.QRCode(
                        version =1,
                        box_size =10,
                        border=6)

    The version parameter specifies the size of the QR code, and its value varies from 1 to 40, the box_size parameter represents the number of pixels each box of QR code should have, and the border parameter controls the thickness of the border. Now add the data to our qr object and make the data fit according to the QR code.

    qr.add_data(data)
    qr.make(fit=True)

    The add_data() function will add the data to the qr object and the fit=True parameter will determine the code automatically. Now create the QR code image based on the qr object, and save it locally.

    image = qr.make_image(fill_color="black", back_color= "white")
    image.save("TGBqrcode.png")

    The make_image() function will generate a QR code image with black color and white background, and the save() function will save the image locally as the name specifies.

    Although you can change the fill_color and back_color of the QR code to any other color, we will suggest keeping them black and white, respectively. Because when we decode or read the QR code with the pyzbar module, it expects a black-and-white QR code.

    Next, let's put the code together and execute it.

    A Python Program to Generate a QR Code

    import qrcode
    
    #initialize qr object
    qr = qrcode.QRCode(
        version =1,
        box_size =10,
        border=6)
    
    data = "https://www.techgeekbuzz.com/"
    
    #add data to qr code
    qr.add_data(data)
    qr.make(fit=True)
    
    #create an image of qr code
    image = qr.make_image(fill_color="black", back_color= "white")
    
    #save it locally 
    image.save("TGBqrcode.png")
    print("QR code has been generated successfully!")

    Output

    QR code has been generated successfully!

    When you execute the above code, it will create an image TGBqrcode.png in the same directory where your Python script is located.

    2. Reading a QR Code in Python

    Now's lets read the data of the QR code that we just generated in the above example. Like before, we will start with importing the required modules.

    from pyzbar import pyzbar
    from PIL import Image

    Now load the QR code image in the script.

    #load qr code imge
    image = Image.open("TGBqrcode.png")

    The Image.open() method will load the QR code TGBqrcode.png image. Now decode the QR code present in the loaded image.

    qr_code = pyzbar.decode(image)[0]

    The decode() function will decode all the codes present in the image and returns a list of all the decoded codes. In our case, the image only contains a single QR code that's why we pick the [0] index or first element from the list. Now let's get the qr_code data and its type.

    #convert into string
    data= qr_code.data.decode("utf-8")
    type = qr_code.type
    text = f"{type}-->, {data}"
    print("----")
    print(text)
    print("----")

    decode() is a string function that decodes the encoded byte utf-8 string to a normal text string. Now let's put the code together and execute.

    A Python Program to Read a QR Code

    from pyzbar import pyzbar
    from PIL import Image
    
    #load qr code imge
    image = Image.open("TGBqrcode.png")
    
    qr_code = pyzbar.decode(image)[0]
    
    #convert into string
    data= qr_code.data.decode("utf-8")
    type = qr_code.type
    text = f"{type}-->, {data}"
    print("----")
    print(text)
    print("----")

    Output

    ----
    QRCODE-->, https://www.techgeekbuzz.com/
    ----

    Conclusion

    In this Python tutorial, we learned how to generate and read a QR code in Python. A QR code is a type of Barcode, and when we say generating a QR code and reading a QR code, we actually generate an image of a QR code and read a QR code from that image only.

    Interested in knowing how to make a barcode reader in Python? Know here .

    People are also reading:

    Leave a Comment on this Post

    0 Comments