Python - Read and Generate Qrcodes Under 10 Lines.
Pratik Mishra
Posted on April 16, 2021
Let's make a Qrcode Generator and reader in Python.
First Install all the dependencies, we'll be using qrcode module for generatin the qrcode and Pillow library as it used by qrcode.
pip install qrcode pillow
First create a folder for your project as in this way all of your things will be properly organised.
Now inside your folder create a python file and name it anything you want to I have named mine as "main.py"
Now let's begin with coding, First import all the required modules:
import qrcode
from PIL import Image
Now create a variable to take input from the user:
text = input('Please enter a text:')
since we have the text ready to be encoded let's create a qrcode from the text:
encoded_text = qrcode.make(text)
# You can name your file to anything you like here in the save method.
encoded_text.save('./qrcode.png')
And there you go you have a your own qrcode ready in just under 10 line of python code.
Now, Let's decode a qrcode in python.
Install the required modules:
pip install pyzbar
Import all the required modules:
from pyzbar.pyzbar import decode
from PIL import Image
Now let's write the remaining code:
#Open your Image in Python using Pillow's Image.open() method.
img = Image.open('./qrcode.png')
decoded_img = decode(img)
print(decoded_img[0][0].decode())
That's it Now you can create your own qrcodes as well as decode them in Python :)
Posted on April 16, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.