
How to create QR Codes in your Python application
Leverage the power of QR Codes in your app using a module and a few simple functions
7 December, 2022
1
1
0
Contributors
QR Codes have become increasingly popular in our day-to-day life. We use them to access websites, make payments, place restaurant orders, authentication, etc. Almost every mobile phone today has a QR Scanner built in.
They are a very useful, fast and easy to use piece of technology and we may want to use them when we build our applications. Today, let us take a look at how using a simple library, we can generate QR Codes and also how to create SVG versions of them.
The qrcode package
We will be using the Python package qrcode to generate the codes. Let us first install it via pip.
pil is a dependency which allows more image functionality.
How to use the module
We can use the make() function on the qrcode module to create a basic, default QR Code:
If we want more control over the behavior of our function, we need to use the QRCode() class:
We can play around with these parameters. More information can be found on the GitHub repository:
We can also encode the image as SVG, for which we must create a factory as an SvgPathImage.
We have at our disposal a number of different functions like creating a styled image and even using the qrcode library from the command line. All of the details can be found on the GitHub page linked above.
Let us write a script
Let's build a small command line app to see the QR Code generation in action
Import modules
First, we import the modules and functions we need
Create our functions to generate QR Codes
Function to create png images
Function to create svg images
Finally let's make it so we can use these functions
If we run our Python script, and give it some data, we will get a QR Code saved to our working directory. If we scan it with our phones, we will get the data which we passed in.
That is it for today. Hope you found this blog helpful and learnt something new today. I have also built a GUI application for generating QR Codes which you can find in my GitHub
Thank you for reading and happy learning!
python
pythontips
develevate
howto