cft

How to Generate QR Code With Python

Generate your custom QR Code with Python in just 10 lines of code.


user

Vibali Joshi

2 years ago | 2 min read

Generate your custom QR Code with Python in just 10 lines of code.

Quick Response Code or a QR Code has gained favor over standard bar codes made of black bars and white spaces due to its capacity to store more information and quick readability. Let us see how you can make code your own QR Code.

Prerequisites

If you have Python3 and pip installed in your machine you can skip this part and head over to the next section.

For the sake of simplicity in this tutorial, we would be using an online IDE called replit

  • Make a free account here.
  • Then click on create a new python project here, give your project a title. Eg: qrcodeGenerator.py

Overview of the process

  • You are required to install a Python package : QRCode.
  • Decide where should the QR Code direct to.
  • After successful installation, you'll write a simple code to generate the QR Code.

Installation

Python offers a QRCode package that does all the underlying work for you.

To install it, type out the following commands in the terminal/shell.

pip install pillow

and

pip install qrcode

Python Code to Generate QR Code

Now you need to decide what information would your QR Code store. For this tutorial, the QR Code would link to makeuseof.com

  1. Implementation:
# main.py
import qrcode

# Link for website
store_info = "<https://makeuseof.com>"

#Creating an instance of qrcode
qr = qrcode.QRCode(
version=1,
box_size=10,
border=4)

qr.add_data(store_info)

qr.make(fit=True)

# Now you need to convert this into an image file and store it
img = qr.make_image(fill='black', back_color='white')

# stores the image as a .png file
img.save('qrcode_generated.png')

import qrcode: Importing the QRCode package

version: The size of the QR Code generated is determined by this option. The larger this value is, the larger the resulting QR Code image will be. The range can be from 1 to 40.

box_size: This value specifies the pixel size of each box.

border: This defines the thickness of the border.

add_data: The method is used to pass the data that the QR Code will direct to.

make and (fit=True): ensures that the entire dimension of the QR Code is utilized.

make_image: you can specify the foreground and background color through this function. Here traditional white and black combination is used.

save:  stores the image as a .png file in the current directory.

  1. Run The Code

2. Click on the qrcode_generated.png file

3. QR Code Generated.

You can check out the complete code here

Generate QR Code With Ease

Using this article you can easily generate QR codes according to your preference with just a few lines of code, without having python installed in your system.

QRcodeRead More:
5 Web Apps Which Show Us How To Make Use Of QR Codes Differently




Upvote


user
Created by

Vibali Joshi

I like to simplify complex programming concepts.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles