Table of Contents
Introduction
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:
qr.make_image(fill_color="white", back_color="blu").save('my_qr_code.png')
Or using the RGB color format:
qr.make_image(fill_color=(55, 95, 35), back_color=(255, 195, 235)).save('my_qr_code.png')
QR code error correction
As we saw in the previous section, when creating the QRCode
object it is possible to control the level of error correction.
The parameter is called error_correction
and you can find it in this piece of code:
qr = qrcode.QRCode(
version=12,
error_correction=qrcode.constants.ERROR_CORRECT_H, # <---
box_size=12,
border=1
)
The possible values it can assume are the following:
ERROR_CORRECT_L
: Corrects about 7% or less of errorsERROR_CORRECT_M
: Corrects about 15% or less of errors. This is the default optionERROR_CORRECT_Q
: Corrects about 25% or less of errors- ERROR_CORRECT_H: Corrects about 30% or less of errors
QR code generation as SVG
In some cases you want to generate a QR code as an SVG.
If this is your goal, don’t worry, in fact the Python qrcode package is able to generate SVG as well.
You can do it directly from the command line using this command:
qr --factory=svg "https://devinsimplewords.com/" > my_qr_code.svg
Or even using Python code:
import qrcode
import qrcode.image.svg
factory = qrcode.image.svg.SvgImage
img = qrcode.make("https://devinsimplewords.com/", image_factory=factory)
You can also create SVGs with styles just like we did for PNGs.
The Python code to use is similar, let’s see it together:
import qrcode
qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgImage)
qr.add_data("https://devinsimplewords.com/")
qr.make(fit=True)
qr.make_image(attrib={'class': 'some-css-class'}).save('my_qr_code.png')
Conclusion
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.
We learned how to create QR code using command line and also using Python code.
We have seen how you can create QR Codes with a few styles and as both PNG and SVG.
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!