Create and Read QR Codes in Python

In this Post, We will See How to Create and Read QR codes in python with built-in libraries.

What are QR Codes

QR Codes are machine-readable code consisting of an array of black and white squares, typically used for storing URLs or other information for reading by the camera on a smartphone.

Create QR Codes

To generate QR Codes in Python. We need 2 Libraries.

PyQRCode and pypng. Install them using these commands.

pyQRCode will be used to generate qr codes while pypng will be used to save generated qr code as png.

pip install PyQRCode
pip install pypng

Once you have installed above libraries. Write Below code to generate qr code.

import pyqrcode
qr = pyqrcode.create("https://code-seekers.com")
img = qr.png("qrcode.png", scale = 5)
print("Qr code generated successfully..")

This code will generate a qr code which will have code-seekers.com link encoded in it.

Read QR Codes

To Read Existing QR Codes using Python, We need pyzbar and Pillow Libraries. Install Them using Below Command.

pip install pyzbar
pip install Pillow

Code

from pyzbar.pyzbar import decode
from PIL import Image
d = decode(Image.open('qrcode.png'))
print(d[0].data.decode())

This code will get the image of QR Code and will decode that to display hidden messege in QR Code.

QR Codes in Python

You can Scan this QR Code to See what I have hided in it for you :).
Thank You For reading the article. If you need any further assistance you can let me know in comments.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *