You’re here because you want to learn how to develop a Python QR Code Generator, don’t worry in this blog post we are going to cover the basics of creating a QR code in Python and will look at a little advanced side of it also, so that you can generator your own QR code with python and flex a little bit.
QR Codes or Quick Response Codes are one of the easiest and fastest ways to share information with other people, we see QR codes in most of the environments we live in, especially if you’re an Indian, as UPI payments are mostly carried out through the QR codes and helps to parse the information much faster for payments.
You can find its use cases in e-tickets, passes, and a hundred other places, there’s no doubt it improves the experience of information transfer, but you’ll be amazed to find out how easy it is to develop a Python QR Code Generator.
Table of Contents
Installing Necessary Python Package
The biggest advantage of developing in Python language is its vast collection of third-party libraries which makes the work so easy for us, we can literally utilize their beautifully developed packages to upscale our application without having to explicitly develop them ourselves. In this case, we have to install Python library for QR code generation, run this pip command from your terminal:
pip install "qrcode[pil]"
or if you are using python3, run
pip3 install "qrcode[pil]"
This command will install Python’s qrcode library to your system along with the pillow library which is used for handling images, and we’re all set to move forward.
Script for Python QR Code Generator
After installing the required Python package, it’s time to write the script for our Python QR code generator. First of all, create a new file named
qr_generator.py in which we are going to do most of our work, let’s start with writing our function for generating the QR Code. We’ll name our function
generate_qr() which will consist of the lines of code required to generate a QR Code. In your
qr_generator.py file write the following lines of code.
img = qrcode.make(data)
Let’s walk through the above piece of code, first, we imported our qrcode module, and then initiated a function named
generate_qr(). This function contains 2 lines of code first it makes a QR code by calling the
.make() method or qrcode class which takes a parameter with which it is going to map the QR code, here we supplied the data as our website URL. In the next line, we called the
.save() method and provided the name of the file we want it to be saved with as an argument.
At last, we called our
generate_qr() function and provided our website URL as an argument to that and that’s it. Now run your Python file by typing this command in the terminal:
or, if you are using python3:
You will see a new PNG file saved in your working directory, with the name
qr_code.png. Open this file, you will see the QR Code. Now to validate this QR Code open the scanner on your phone and scan this QR Code it will direct you to our website only. It was fun, wasn’t it?
Advance Styling Options
Till now, we have made a basic QR Code with little to no lines of code. Let’s start to spice things up a little bit and tweak the looks of our QR Code. We’re going to work in the same file
qr_generator.py with same function
generate_qr(). First, we are going to use the QRCode class from the qrcode package, to get more advanced controls over handling the QR Code.
qr = qrcode.QRCode(
# more code to be added
Here, we can control by the Box Size of QR Code by setting the value for
box_size parameter and same for border of QR Code with
qr = qrcode.QRCode(
# Adding data to the QRCode instance
img = qr.make_image(fill_color="black", back_color="white")
In this block of code, we added the data to
qr instance by calling the
.add_data() method then we use
.make() method to make the image with fit being true. In the next step, we called
.make_image() with arguments black for
fill_color which will account for the squares on forefront of our QR Code and white for
back_color which will be the background of our QR Code. Finally, we save our QR code with
img.save("qr_code.png") by providing the file name as argument.
Now comes the customizing part, let’s try different colors for our QR Code, i want it to have a purple background and white fill color for our squares. Let’s modify the code for the same.
# ... leading code
img = qr.make_image(fill_color="white", back_color="purple")
# ... ending code
As we discussed, to change the fill and background color we need to change
back_color parameters in our code line, we changed them to white and purple respectively. Now run the file, and here’s the output you will see when you open you
Well, it doesn’t look that bad, does it? Give it a try and customize it’s width, border and colors according to your needs.
To wrap up, we installed the library qrcode and used it to create beautiful QR Code with python. First we looked at the simple approach, and later on tried customizing it’s width, border and colors. Hope you learned and tried to implement the things we discussed.