QR code in Python with Source code

QR code in Python with Source code

Table of Contents

Introduction

QR code is a type matrix barcode. It need to be scanned with QR scanner. When we scan some QR code it takes to the specific url.

QR was first designed in Japan for auto industries. It is similar to a barcode. As barcode has a stripe line while QR has a sketch of stripes. QR can carry more information than barcode. A QR has a square shape. The QR codes contains website URLs, email addresses, contact data and text information. So the QR are very useful for sharing information.

In this tutorial, we will be learning how to create QR code using Python. It is very easy for generating QR using python. We just need few lines of code. It is not that hard at all.

First thing is that you have to import pyqrcode. You need to install pyqrcode packages. It is a simple QR code generator.

import pyqrcode

If you need the QR in png format, you must install pypng module. You can just type import png and install the module.

Import png

Info QR code

For generating QR you must provide the URL of the website or any other.

url = pyqrcode.create('http://code-mentor.online')

Creating and saving your QR in svg format. You can use following code.

url.svg('code-url.svg', scale=8)

You must have install pypmg module. For generating QR in png format. Use this code.

 url.png('code.png', scale=2)

Printing the QR use this below.

print(url.terminal(quiet_zone=1))

As you execute these code you will get a QR code for the target URL. You will see your QR on the command line. The GUI interface is not in use. It will execute on the command line. Scan with your phone to test this output.

QR code

Assembling the full code. See the code below.

import pyqrcode  
import png  
url = pyqrcode.create('http://code-mentor.online')   
url.svg('code-url.svg', scale=8)
url.png('code.png', scale=2)
print(url.terminal(quiet_zone=1))

Requirements

  • Python modules.
  • Code editor or IDE for python.
  • Python knowledge.