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.
1 2 3 |
pip install qrcode[pil] |
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.
1 2 3 4 5 6 7 8 9 |
import qrcode # Create an image from the QR code instance qr = qrcode.make("Hello world") # Save the image qr.save('myfirstqr.png') |
- 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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# import qrcode module import qrcode # create QR code instance qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=5, border=5 ) # Enter data that you want to store qr_data = 'Welcome To Simplified Python' # Add data qr.add_data(qr_data) qr.make(fit=True) # Create an image from the QR code instance img = qr.make_image(fill_color='blue', back_color='white') # Save the image img.save('qr.png') |
- 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.
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:
- Python Tic Tac Toe Using Artificial Intelligence
- How To Add Code To GitHub Using PyCharm
- PDF To Text Python – Extract Text From PDF Documents Using PyPDF2 Module
- Python Get Files In Directory Tutorial