You are 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 look at one side of it a bit advance. also, so you can generate your own QR code with python and be a little flexible.
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 are Indian, as UPI payments are mostly made through QR codes and helps analyze the information much faster for payments.
You can find its use cases in e-tickets, passes and a hundred other places, there is no doubt that it improves the information transfer experience, but you will be amazed to find out how point it is easy to develop a Python QR code generator. .
Installation of Python package required
The biggest advantage of developing in Python language is its vast collection of third-party libraries which makes the job so easy for us, we can literally use their beautifully developed packages to upgrade our application without having to explicitly develop them ourselves. In this case we need to install the Python library for QR code generation, run this seed order from your terminal:
pip install "qrcode(pil)"
or if you are using python3, run
pip3 install "qrcode(pil)"
This command will install the Python qrcode library on your system with the pillow library that is used to manage images, and we are all ready 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, create a new file named qr_generator.py
in which we are going to do most of our work, let’s start by writing our QR Code generation function. We will name our function generate_qr()
which will be composed of the lines of code necessary to generate a QR Code. In your qr_generator.py
file, write the following lines of code.
# qr_generator.py
import qrcode
def generate_qr(data):
img = qrcode.make(data)
img.save("qr_code.png")
generate_qr("https://pypixel.com")
Let’s go through the piece of code above. We first imported our qrcode module, then launched a function named generate_qr()
. This function contains 2 lines of code it first creates a QR code by calling the .make()
qrcode method or class which takes a parameter with which it will map the QR code, here we have provided the data as our website URL. In the next line, we called the .save()
and provided the name of the file we want it saved with as an argument.
Finally, we called our generate_qr()
function and provided our website URL as argument and that’s it. Now run your Python file by typing this command in the terminal:
python qr_generator.py
or, if you use python3:
python3 qr_generator.py
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?
Advanced styling options
So far we’ve created a basic QR code with few to no lines of code. Let’s start by spicing things up a bit and tweaking the appearance of our QR Code. We will work in the same file qr_generator.py
with the same function generate_qr()
. First, we’ll use the QRCode class from the qrcode package, to get more advanced controls on QR code handling.
# qr_generator.py
import qrcode
def generate_qr(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# more code to be added
generate_qr("https://pypixel.com")
Here we can control the size of the QR code box by setting the value of box_size
parameter and the same for the border of the QR Code with border
setting.
# qr_generator.py
import qrcode
def generate_qr(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Adding data to the QRCode instance
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("qr_code.png")
generate_qr("https://pypixel.com")
In this code block, we added the data to qr
example by calling the .add_data()
method then we use .make()
method for the image to be true. In the next step we called .make_image()
with black arguments for fill_color
which will represent the squares in the foreground of our QR Code and the 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 an argument.
Now comes the customization part, let’s try different colors for our QR code, I want it to have a purple background and a white fill color for our squares. Let’s modify the code for the same.
# qr_generator.py
# ... 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 fill_color
And back_color
parameters in our line of code, we changed them to white and purple respectively. Now run the file and this is the result you will see when you open it qr_code.png
deposit.
Well, that doesn’t sound so bad, does it? Try it and customize its width, border and colors according to your needs.
Conclusion
To conclude, we installed the qrcode library and used it to create a beautiful QR Code with python. We first looked at the simple approach and then tried to customize its width, border and colors. I hope you learned and tried to implement the things we discussed.
Learn more