How to generate QR code in python – Quick start guide

How to generate QR oce in Python

QR code was firstly designed in 1994 in Japan in the automotive industry. It is a type of barcode matrix that can help us to store the details. In this tutorial, we are going to see how to generate QR code in python.

we can store every detail of us in the form of QR code that may be personal details, product details, other info. we can share the QR code with others easily. To read QR codes you need to install QR code scanners from playstore or iOS store.

How to generate QR code in python

This is a very simple step by step guide to generating QR code in python. we will discuss

  • Generate QR code
  • Generate QR code with links
  • Styling your QR code
  • Generate QR code with user input data
  • Read QR code data using python

Generate QR code

Generate a QR code in python is very simple. Before we generate our QR code, we need to install these below packages.

pip install qrcode #To generate QR code
pip install Pillow #To manage Images

Now, create a new python file and write the below lines.

import qrcode

qr = qrcode.make('hello') # 'hello' is a data we can write anything here.
qr.show()
qr.save('hello.png')       # hello.png is image file name.

we import qrcode module, and create qrcode.make(‘hello’) generate the QR code. qr.show() will show the QR code directly in our system. qr.save(‘hello.png’) save the QR code image in our local directory.

Now run the code in your prompt, it will open and generate QR code. Congratulations we generate QR code in python with few lines.

Generate QR code with links

lets take this a little bit forward, generate a QR code with hyperlinks.

import qrcode

data = 'www.devpyjp.com'
qr = qrcode.make(data)
qr.save('devpyjp.png')
qr.show()

Now run this file on prompt and get a new QR code with “devpyjp.png”. This QR code holds the link that we mentioned above.

Styling your QR code

well, we generate a QR code with default colors like background white and foreground with black. But you may ask me, I don’t need this color Jp, I need a QR code with my favorite colors. can you help me, Jp?

yes, of course! we can dude. let’s start our styling QR code in python.

import qrcode

qr = qrcode.QRCode(
    version=5,
    box_size=5,
    border=2
)
data = 'www.devpyjp.com'
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color='green',back_color='white')
img.save('devpyjp.png')

Here, qrcode.QRCode() will take 3 parameters like version, box_size, border.

version represents the size of the QR code. It is a range of 1 to 40.

border represents the border of the QR code.

box_size represents the box size of the QR code.

qr.make_image(fill_color=’green’,back_color=’white’) take 2 parameters. Here we can stylish our QR code.

fill_color represents the foreground color i,e pattern color of QR code.

back_color represents the background color.

Generate QR code with user input data

Ok Jp, I am happy with the above lines. as of now I stylish my QR code but I need to take it to the next level. I want to generate a QR code by taking user input data that must be unique. can you help me Jp?

ok, dude! I am here to help you. let’s start with that also.

import qrcode
qr = qrcode.QRCode(
    version=5,
    box_size=5,
    border=2
)
name = input('Enter Name:')
age = int(input('Enter Age:'))
Education = input('Enter Ed.Qualification:')
data = {'name':name,'age':age,'edu':Education}
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color='black',back_color='white')
img.save('student.png')
img.show()

It will ask you for the inputs as you mentioned above lines. we take all the data in the dictionary format and store it into QR code. it saves the image as student.png in your local directory.

I think I answered your above question buddy.

Read QR code data using python

Ok, Jp! well, you helped me a lot. I stored my data in my QR code. But how can I verify it? you know its very general in our society.

we see in local pubs and college events and offices, they scan your Id which consists of QR code, whether you are a verified a person or not for that event or authorized employee.

let’s read our QR code data using python. we are taking the above student.png image.

For that, we need to import 3 other libraries as below.

import qrcode
from pyzbar.pyzbar import decode
from PIL import Image
import ast
data = decode(Image.open('student.png'))
info = data[0].data.decode('ascii')
data = ast.literal_eval(info)
#print(type(data))
print(data['name'].upper())
print(data['age'])

pyzbar helps us to decode our QR code.

ast help us to convert string to dictionary format.

Pillow helps us to manage our images.

our QR code data encoded as binary code so we need to decode it by using pyzbar. Our data is in string format but we have to separate our data like name, age, and Edu. so we need to convert that output string to a dictionary. so we use ast.

It will print the data stored in the sudent.png image. That’s it, guys..!

I hope you definitely love this tutorial, check this Guessing game in python tutorial. subscribe to our newsletter to get FREE Python projects PDFs.

Don't miss out!
Subscribe To Newsletter

Receive top Machine learning & Python news, lesson ideas, project tips, FREE Ebooks and more!

Invalid email address
Thanks for subscribing!

Leave a Reply