Table of Contents
- QR code generation in Python – qrcode package
A QR code is a barcode that can be read by a digital device such as a smartphone.
QR codes are used to keep track of information such as websites or social profiles.
If you are wondering if it is possible to generate a QR code using Python, the answer is yes and it is simpler than you think.
A QR code is made up of a series of pixels in a square-shaped grid.
To read the information contained within it, simply have a simple reader installed on your smartphone that can be downloaded for free from all mobile stores.
This post will show you how to create your own QR code from scratch using Python without any kind of limitation.
QR code generation in Python – qrcode package
Creating a QR code in Python is very simple.
For this tutorial we will use qrcode, a Python package that can be used both from the command line and from code.
In both cases, very few instructions are needed to generate the QR code and more importantly, it has no limitations of any kind!
Read on to learn how to install it and how to use it!
qrcode package installation
Since we are installing an external package, I recommend that you create a virtualenv.
If you don’t know how to do it, here there is a post with the procedure on how to create a virtualenv.
Regardless of whether you have created the virtualenv or not (I recommend it) you can install qrcode with the following command:
pip install qrcode[pil]
If you need to install a specific version, for example 7.0, use this command:
pip install qrcode[pil]==7.0
Feel free to replace 7.0 with the version you need.
Last but not least, an important tip for macOS users:
pip install qrcode"[pil]"
Use the command above to install qrcode package.
How to generate a QR code in Python
Generate QR code via command line
To generate a QR code using the command line, activate the virtualenv and type the following command:
qr "https://devinsimplewords.com/" > my_qr_code.png
This will generate a QR code called
my_qr_code.png which if scanned will take you to the devinsimplewords homepage.
As you have probably understood, you just need to change the site/social profile you want to point to create your personalized QR code.
The generic syntax is this:
qr "<web site or social profile>" > name_of_qr_code.png
The output of the previous command looks like this:
Simple QR code generation
To generate a simple QR code using code, activate the virtualenv and type the following lines:
import qrcode qrcode.make("https://devinsimplewords.com/").save("my_qr_code.png")
The output of this code is exactly the same as that generated by the command line.
Advanced QR code generation
The sections showed us how to create a simple QR code, but the qrcode package also allows us to customize our output with different options.
Let’s see how to do it:
import qrcode qr = qrcode.QRCode( version=12, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=12, border=1 ) qr.add_data("https://devinsimplewords.com/") qr.make() qr.make_image(fill_color="white", back_color="black").save('my_qr_code.png')
You can change the background color and the pixels by modifying this line of code:
Or using the RGB color format:
qr.make_image(fill_color=(55, 95, 35), back_color=(255, 195, 235)).save('my_qr_code.png')
Here we are at the end of this post, as always I hope this article will be useful to you and that now you know everything about the Python package qrcode.
If you find that something is unclear or you have a problem that you don’t know how to solve, leave me a comment below and I will try to help you as soon as possible.
If this topic is clear to you, take a look at the latest posts!