to QR Codes
“QR” in QR code stands for quick response. QR codes are an emerging and easy way to retrieve information without typing or searching anything on your phone. These codes are essentially a pattern of black and white squares, with scannable digital codes embedded in them. Nowadays, restaurants use these QR Codes to present the menu to their customers, shops and marts use them as a form of contactless digital payments, event management teams use them as a quick check-in to their events and conferences, and so forth and so on.
Generating QR Codes
As was mentioned earlier, QR Codes are developed as a pattern or a grid of small black and white squares that store information in binary form, that is, as 0s and 1s. Information is encoded within the code in special arrangements with the feature of customizing the colors, background, and frames, as long as the pattern remains the same.
The following is an example of a QR code that has been generated using the Python “qrcode” package:

Scanning the above code with a scanner that has QR code scanning capabilities will take you to my TDS profile, where you can easily access beginner-friendly Python projects.
In this article, we will go through the Python Package qrcode, installing it and exploring its different functions to design QR Codes.
Installing the Package
Before starting the project, we will install the relevant packages. I am using PyCharm IDE for this task. In order to install the “qrcode” Python package, I will go to the Python terminal and type the following:
pip install qrcode 
Installing the QRCode package will allow us to create QR codes as PNG files and render them into the Python console. However, if we require more image-related functionality, we should install the Pillow package for image processing capabilities.
pip install "qrcode[pil]" 
Importing Library and Generating a Simple QR Code
Now we will begin the coding. First, we will import the library and include an alias name for our ease. As can be seen in the QR Code Python documentation, the QR Code image for a URL can easily be generated and saved as a PNG using the following lines of code:
import qrcode as qr
img = qr.make("https://pypi.org/project/qrcode/")
img.save("Python QR Code Documentaion.png") 
This is a simple QR code that has been created using the make() function and saved as a PNG file using the save() function.
Advanced Functionality
For advanced image processing features, we will use the QRCode Class in the qrcode Python Package.
Classes and Objects are a useful concept in programming. Python classes provide a blueprint for the creation of objects that share similar features, their attributes (variables), and methods (functions). We will use the Python Class constructor to create QR codes as objects of that class.
Below is the generic code that allows us to create and save QR codes:
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('https://towardsdatascience.com/implementing-the-coffee-machine-project-in-python-using-object-oriented-programming/')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("Understanding Classes and Objects.png") 
The above code has created and saved the QR code as a PNG. If you scan the above code, you will land on an interesting Python Tutorial that explores how the concept of Python classes and objects is implemented in real-world projects.
Now, let us deep dive into the generic code above and explore the functions and play along with their variations.
Object Creation
The first line of code after importing the qrcode library creates the object, with its attributes and methods enclosed within the round brackets.
qr = qrcode.QRCode(
...
...
...
...
...
) Here, qrcode refers to the Python package and QRCode() refers to the Class.
Defining the Version
Next is to define the version. We can set the integer from 1 to 40, and this will result in different sizes of the QR Code.
import qrcode
qr = qrcode.QRCode(
version=1,
...
...
...
...
) Let’s create a QR Code with the version variable set to 5.

Now let us set the version parameter to 15:

As can be seen in the two QR Codes above, the version attribute determines the size of the QR Code. The smallest version 1 outputs a QR Code as a 21×21 grid.
Error Correction
The next attribute that we will go through is error_correction. The Error Correction attribute deals with the data redundancy, so that even if the QR Code is damaged, it still remains scannable. There are 4 different levels of error_correction:
- Error Correct L: This provides the lowest level of error correction, that is 7%
- Error Correct M: This provides a moderate level of data correction, 15% or less, thus providing a balance between error correction and data size
- Error Correct Q: This provides 25% or less of error correction
- Error Correct H: This provides a high level of error correction and is suitable for applications that may have serious damage, and where data size is not a concern.
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
...
...
) The more the percentage value of error_correction, the more easily it becomes scannable, even when parts of it are damaged. On the flip side, the QR Codes become larger in size, and this poses a problem when data compression is a main requirement.
Let us create QR Codes with different error_correction types.




As can be seen from the variety of the QR Codes generated above, as the error_correction increases, the complexity of the QR Codes increases, meaning the data size increases, while the data redundancy also increases.
Box Size
The next attribute we will explore and play with is the box_size. The box_size in the Python qrcode library refers to the number of pixels the QR Code will have.
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
...
) Let us see how our QR Code changes with different values of the box_size:



The above images demonstrate the difference when the pixel values changes, although they might be negligible to the naked eye when it comes to small differences.
Border
The last attribute that we need to define to create the object is the border. This refers to the surrounding box around the code. The minimum value is 4, and we can increase it as much as we like. Let us see how the code changes with changes in this attribute:


We can see the difference in the border of the above images, which can easily be customised with the border variable.
Adding Data
Once our object has been created, with particular values of version, error_correction type, box_size and border, we will now add the data that needs to be encoded as QR Code. This data can be a string, a URL, a location, contact information, etc. This can be easily done through the add_data() method of this class.
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=1,
border=10,
)
qr.add_data('https://towardsdatascience.com/building-a-facial-recognition-model-using-pca-svm-algorithms-c81d870add16/')
qr.make(fit=True) The last line of code above calls qr.make(fit=True). This automatically fits the QR Code to the data that has been given, after analyzing it. It uses the smallest possible QR Code to accommodate the data, and does not require us to manually set the sizing attributes.
Generating the Image Object
Once the QR Code object is created, we will use PIL to generate the image while defining its colors, as well as saving that image in a suitable way. In the following example, we will set the background to black and fill color to pink in our QR Code, as can be seen in the code below:
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=1,
border=10,
)
qr.add_data('https://towardsdatascience.com/using-python-to-build-a-calculator/')
qr.make(fit=True)
img = qr.make_image(fill_color="pink", back_color="black")
img.save("calc.png") 
Conclusion
In this article, we explored the Python package QR Code, which includes the blueprint for creating QR codes and saving them in different file formats. It also includes advanced customisation of the QR code generation, which we have gone through with the understanding of classes and objects. This was an easy and beginner-friendly Python Tutorial that required some surface-level knowledge of classes and objects.
Source link
#Generate #Codes #Python









