to QR Codes
“QR” in QR code stands for fast response. QR codes are an rising and simple technique to retrieve data with out typing or looking something in your cellphone. These codes are primarily a sample of black and white squares, with scannable digital codes embedded in them. These days, eating places use these QR Codes to current the menu to their clients, outlets and marts use them as a type of contactless digital funds, occasion administration groups use them as a fast check-in to their occasions and conferences, and so forth and so forth.
Producing QR Codes
As was talked about earlier, QR Codes are developed as a sample or a grid of small black and white squares that retailer data in binary kind, that’s, as 0s and 1s. Data is encoded inside the code in particular preparations with the function of customizing the colours, background, and frames, so long as the sample stays the identical.
The next is an instance of a QR code that has been generated utilizing the Python “qrcode” package deal:
Scanning the above code with a scanner that has QR code scanning capabilities will take you to my TDS profile, the place you’ll be able to simply entry beginner-friendly Python tasks.
On this article, we are going to undergo the Python Bundle qrcode, putting in it and exploring its completely different features to design QR Codes.
Putting in the Bundle
Earlier than beginning the undertaking, we are going to set up the related packages. I’m utilizing PyCharm IDE for this activity. With a purpose to set up the “qrcode” Python package deal, I’ll go to the Python terminal and kind the next:
pip set up qrcode

Putting in the QRCode package deal will enable us to create QR codes as PNG recordsdata and render them into the Python console. Nevertheless, if we require extra image-related performance, we must always set up the Pillow package deal for picture processing capabilities.
pip set up "qrcode[pil]"

Importing Library and Producing a Easy QR Code
Now we are going to start the coding. First, we are going to import the library and embrace an alias identify for our ease. As could be seen within the QR Code Python documentation, the QR Code picture for a URL can simply be generated and saved as a PNG utilizing the next traces of code:
import qrcode as qr
img = qr.make("https://pypi.org/undertaking/qrcode/")
img.save("Python QR Code Documentaion.png")

This can be a easy QR code that has been created utilizing the make() perform and saved as a PNG file utilizing the save() perform.
Superior Performance
For superior picture processing options, we are going to use the QRCode Class within the qrcode Python Bundle.
Courses and Objects are a helpful idea in programming. Python lessons present a blueprint for the creation of objects that share comparable options, their attributes (variables), and strategies (features). We’ll use the Python Class constructor to create QR codes as objects of that class.
Under is the generic code that permits us to create and save QR codes:
import qrcode
qr = qrcode.QRCode(
model=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(match=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("Understanding Courses and Objects.png")

The above code has created and saved the QR code as a PNG. In case you scan the above code, you’ll land on an attention-grabbing Python Tutorial that explores how the idea of Python lessons and objects is applied in real-world tasks.
Now, allow us to deep dive into the generic code above and discover the features and play together with their variations.
Object Creation
The primary line of code after importing the qrcode library creates the thing, with its attributes and strategies enclosed inside the spherical brackets.
qr = qrcode.QRCode(
...
...
...
...
...
)
Right here, qrcode refers back to the Python package deal and QRCode() refers back to the Class.
Defining the Model
Subsequent is to outline the model. We are able to set the integer from 1 to 40, and this can lead to completely different sizes of the QR Code.
import qrcode
qr = qrcode.QRCode(
model=1,
...
...
...
...
)
Let’s create a QR Code with the model variable set to five.

Now allow us to set the model parameter to fifteen:

As could be seen within the two QR Codes above, the model attribute determines the dimensions of the QR Code. The smallest model 1 outputs a QR Code as a 21×21 grid.
Error Correction
The subsequent attribute that we are going to undergo is error_correction. The Error Correction attribute offers with the information redundancy, in order that even when the QR Code is broken, it nonetheless stays scannable. There are 4 completely different ranges of error_correction:
- Error Appropriate L: This offers the bottom stage of error correction, that’s 7%
- Error Appropriate M: This offers a reasonable stage of information correction, 15% or much less, thus offering a steadiness between error correction and information dimension
- Error Appropriate Q: This offers 25% or much less of error correction
- Error Appropriate H: This offers a excessive stage of error correction and is appropriate for purposes which will have severe harm, and the place information dimension isn’t a priority.
qr = qrcode.QRCode(
model=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
...
...
)
The extra the share worth of error_correction, the extra simply it turns into scannable, even when components of it are broken. On the flip aspect, the QR Codes turn out to be bigger in dimension, and this poses an issue when information compression is a fundamental requirement.
Allow us to create QR Codes with completely different error_correction varieties.




As could be seen from the number of the QR Codes generated above, because the error_correction will increase, the complexity of the QR Codes will increase, which means the information dimension will increase, whereas the information redundancy additionally will increase.
Field Measurement
The subsequent attribute we are going to discover and play with is the box_size. The box_size within the Python qrcode library refers back to the variety of pixels the QR Code may have.
qr = qrcode.QRCode(
model=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
...
)
Allow us to see how our QR Code adjustments with completely different values of the box_size:



The above photos reveal the distinction when the pixel values adjustments, though they could be negligible to the bare eye relating to small variations.
Border
The final attribute that we have to outline to create the thing is the border. This refers back to the surrounding field across the code. The minimal worth is 4, and we are able to improve it as a lot as we like. Allow us to see how the code adjustments with adjustments on this attribute:


We are able to see the distinction within the border of the above photos, which might simply be customised with the border variable.
Including Knowledge
As soon as our object has been created, with specific values of model, error_correction sort, box_size and border, we are going to now add the information that must be encoded as QR Code. This information generally is a string, a URL, a location, contact data, and so forth. This may be simply performed via the add_data() technique of this class.
import qrcode
qr = qrcode.QRCode(
model=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(match=True)
The final line of code above calls qr.make(match=True). This robotically matches the QR Code to the information that has been given, after analyzing it. It makes use of the smallest potential QR Code to accommodate the information, and doesn’t require us to manually set the sizing attributes.
Producing the Picture Object
As soon as the QR Code object is created, we are going to use PIL to generate the picture whereas defining its colours, in addition to saving that picture in an acceptable approach. Within the following instance, we are going to set the background to black and fill coloration to pink in our QR Code, as could be seen within the code beneath:
import qrcode
qr = qrcode.QRCode(
model=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(match=True)
img = qr.make_image(fill_color="pink", back_color="black")
img.save("calc.png")

Conclusion
On this article, we explored the Python package deal QR Code, which incorporates the blueprint for creating QR codes and saving them in several file codecs. It additionally contains superior customisation of the QR code era, which now we have gone via with the understanding of lessons and objects. This was a simple and beginner-friendly Python Tutorial that required some surface-level data of lessons and objects.

