How To Generate QR Code With Python

Hey guys welcome to my new tutorial How To Generate QR Code With Python. In this tutorial, you will learn  to generate QR code using python and many thing. So follow this till the end.

Before going to our main topic, we need to understand some basics idea of QR code. So basically QR(Quick Response) Code is a two-dimensional version of the barcode, known from product packaging in the supermarket. QR Codes are gaining popularity because the technology is “open source”, i.e. available for everyone. A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera.If you want to know more about barcode then check this link

So now let’s jump into main part of this tutorial.

How To Generate QR Code With Python

Python has a qrcode library for generating QR code –

  • qrcode is a QR Code image generator.

Now we will see how to generate QR code in python using qrcode module. So let’s gets started.

Installing qrcode Module

So to use qrcode module, you have to install this on your system. Write following command and run it on your command prompt.

You have to install qrcode package with  pillow support. Without pillow you will not able to save your QR code in image file.  Without Pillow you will get the message error ‘ImportError: No module named Image‘ during the creation of an image with the QR Code library.

Creating QR Code as an image

Now write the following code on your python IDE.

  • The first thing you need to do is to import qrcode.
  • Then you have to call the make() function that is used to create the QR code and inside make() function you have to assign the data that you want  to  store.
  • Now finally save this image. You can change the format of image as your requirement. You can save image in png, jpeg or bmp format.

Output

How To Generate QR Code With Python
How To Generate QR Code With Python

If you have any QRCode scanner app , then you can easily scan this QR code and can see the data of this QR code.

  This is very simple method to create a QR code. Now we will see an another method of creating QR code in python. So write the following program.

  • First of all import qrcode module.
  • Then create an instance of QR code. 
  • Now pass the parameters. The first parameter is version, it takes an integer from 1 to 40 to control the size of the QR code.
  • The second parameter is error_correction
  •  If the QR code is damaged or dirty then  QR code has capability to restore data that is called error correction . Four error correction levels are available with this library, they’re stored in the qrcode.constants object:
  • ERROR_CORRECT_L: About 7% or less errors can be corrected.
  • ERROR_CORRECT_M: (default) About 15% or less errors can be corrected.
  • ERROR_CORRECT_Q: About 25% or less errors can be corrected.
  • ERROR_CORRECT_H: About 30% or less errors can be corrected.
  • They should be provided as value of the error_correction property during the creation of the QR Code.
  • The third parameter is box_size, it actually controls how many pixels each “box” of the QR code is.
  • The fourth parameter is border, it controls how many boxes thick the border should be (the default is 4, which is the minimum according to the specs).
  • Now we need to create a data variable to store the data.
  • Now add this data to the instance of QR code. add_data() method is used to add the data.
  • make(fit=True) the fit parameter basically controls, if you want to automatically exhaust the QR code dimension size.
  • Now create an image from the QR code instance. fill_color and back_color can change the background and the painting color of the QR, when using the default image factory.
  • Finally save the image.

Now let’s see how our QR code looks like.

How To Generate QR Code With Python
How To Generate QR Code With Python

So guys this was all about the How To Generate QR Code With Python tutorial. I hope, you found it helpful but if you have any query regarding this then feel free to ask. I will come with a new tutorial till then stay tuned with SIMPLIFIED PYTHON. THANKS. 

Related Articles:

Leave a Comment