Stipulations: Python fundamentals
Variations: Python 3.10, qrcode 7.3.1, Pillow 9.2.0
Learn Time: 40 minutes
Introduction
Have you ever ever questioned how QR codes work or how procedural pictures are generated? Have you ever ever wished to ship somebody an internet site hyperlink in a a lot cooler manner? If you happen to stated sure to any of those questions, you are in luck!
On this fast tutorial, we are going to learn to create a QR code in Python with qrcode, pillow, and simply 5 traces of code.
Let’s bounce in!
What Is a QR Code?
The QR code, brief for Fast Response code, was initially invented in 1994 by a Japanese tech firm. It’s a 2D barcode containing black patterns on a white background. Nonetheless, that is no abnormal scribble: QR codes are able to storing large quantities of knowledge in a deceivingly small quantity of area. These black rectangles can retailer hyperlinks, textual content, mainly something you need… and could be accessed just by scanning from any cell system!
A QR code is necessary because it offers customers a easy strategy to entry one thing on a non-conventional supply (e.g., on a bit of paper). Placing a QR code on a bit of paper is a much better and sooner expertise for the person than putting an internet site hyperlink. Resulting from this, QR codes are actually changing into extra generally used than UPC barcodes and are discovered on restaurant menus, enterprise playing cards, and even Superbowl adverts!
Sufficient about QR codes, let’s learn to create one!
Setting Up
First, go to the Python code editor of your alternative (we advocate VS Code), and create a brand new file referred to as qr_code.py. That is the place we shall be writing our code.
Observe: You may name your file any title besides qrcode.py. It’s because qrcode.py is a file that already exists as a part of the qrcode
library that we’ll use, and calling your file that can overwrite the library capabilities.
To start out, we have to set up the 2 libraries:
- The
qrcode
library: This library lets us carry out all of our QR code associated operations. - The
pillow
library: This library helps us course of and save pictures.
To put in qrcode
and pillow
, run this command contained in the VS Code terminal:
pip set up qrcode pillow
For this tutorial, we’re utilizing qrcode model 7.3.1 and Pillow model 9.2.0.
Subsequent, add this line of code to the primary line of qr_code.py:
import qrcode
This line of code makes positive that the 2 libraries can be utilized in the remainder of our code, since Python code runs from prime to backside in a file. We simply have to import qrcode
, as a result of pillow
is implicitly imported.
Creating the QR Code
First, we wish a hyperlink that we need to showcase. Let’s use a traditional YouTube video.
We will retailer this YouTube URL right into a variable referred to as website_link
:
website_link = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
Subsequent, we need to create an occasion of qrcode
. Since it is a Python library, we will name the bundle constructor to create a qrcode
object, personalized to our specs.
On this instance, we are going to create a QR code with a model of 1, and a field dimension and border dimension of 5.
qr = qrcode.QRCode(model = 1, box_size = 5, border = 5)
- The
model
parameter is an integer from 1 to 40 that controls the scale of the QR code. - The
box_size
parameter controls what number of pixels every “field” of the QR code is. - The
border
parameter controls what number of containers thick the border needs to be.
As an train, attempt taking in these parameters as enter, and explaining to the person find out how to set this up, to allow them to create the QR code to their very own specs.
Go to documentation for extra details about the parameters in qrcode.QRCode(...)
.
Then, the information (particularly, the hyperlink we specified earlier than) is added to the QR code, utilizing .add_data()
. The QR code is then generated utilizing .make()
:
qr.add_data(website_link)
qr.make()
Lastly, we save this created QR code in an img
pillow object utilizing qr.make_image()
:
img = qr.make_image(fill_color = 'black', back_color = 'white')
- Setting the road coloration
fill_color
to black. - Setting the background coloration
back_color
to white.
Lastly, we’ve to retailer and save the file. We will do that utilizing pillow’s save()
command. We specify the file title contained in the brackets, which is youtube_qr.png
in our case.
img.save('youtube_qr.png')
Now we’re accomplished! Right here’s the entire code:
import qrcode
website_link = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
qr = qrcode.QRCode(model = 1, box_size = 5, border = 5)
qr.add_data(website_link)
qr.make()
img = qr.make_image(fill_color = 'black', back_color = 'white')
img.save('youtube_qr.png')
It is best to see the youtube_qr.png picture pop up on the left-hand facet of VS Code, and you’ll open it to see what it seems like.
You may add this QR code to wherever you want, in your web site or in an electronic mail!
Enhancements
To enhance this, we might do a few issues:
- Enable the web site hyperlink to be typed in utilizing
enter()
operate. - Enable customers to customise the QR code generated.
- Automate the method to create a number of QR codes.
- Embrace extra capabilities (or object parameters) of the
qrcode
library. - Attempt altering the colours and kinds of the generated QR codes utilizing totally different drawer modules and fill colours.
- Use an software library (like Tkinter) so as to add a person interface.
- Try different QR code libraries like
pyqrcode
.