Hey there! Today we are going to learn how to get your own QR codes using the qrcode module in Python.
Seems interesting right? Let’s begin!
Introduction to QR (Quick Response) code
QR codes are able to store lots of data and when scanned, it allows the user to access the information instantly.
It stores all the data as a series of pixels in a square-shaped grid. In general, we use QR codes for the following purposes:
- Link app download link
- Accounts login details
- Making payments
The main components of a standard QR code are the three large squares outside the QR code. Once the QR Reader identifies them then it knows the whole information contained within the square.
Recommended read: How to create fake people details using the faker module?
Creating OR codes from scratch using the qrcode module
The first thing we do is importing
qrcode module and then creating a
qr object using the
The next step we have to code for is to add the data into the QR code using the
add_data function. We pass the data we want in form of a string.
Next, we use the
make function to build the QR code. The next step is to get the image of the QR code we build.
To create and save the QR code in form of an image we will make use of the
save function respectively.
In the same function, we add the image path/name of the image. The code for the same is shown below.
qr = qrcode.QRCode()
qr.add_data('This is my first QR code.')
img = qr.make_image()
The QR code generated is displayed below.
The image below shows the result when the QR code saved was scanned through my device.
Customizing the QR code
We can also customize the design and structure of the QR code by adding some properties in the qr object created earlier using the
Some properties that we are going to add in the object are as follows:
version: This determines the size of the QR code and its value ranges from 1 to 40 ( 1 being the smallest obviously)
box_size: This determines the no. of pixels need to be there in the QR box
We also added a few properties in the
make_image function to change the color of the background and QR code by using the
fill_color properties respectively.
The code for the generation of such QR codes is shown below:
qr1 = qrcode.QRCode(version=1,box_size=10)
qr1.add_data('My first customized QR code')
img1 = qr1.make_image(fill_color="red", back_color="lightblue")
The output customized QR code is shown in the image below:
When scanned from my own device the result were accurate which is displayed below:
Congratulations! Now you can build QR codes for anything you want on your own. You can also add links instead of simple text for the QR code to reach a site when the QR code is scanned.
Hope you learned something! Happy coding!