Generating QR Codes
00:00
Using Python to generate a basic QR code. To begin with, you are going to create a black and white QR code that encodes some content. To follow along with this course, you’ll need to install Segno, which is a popular Python library for generating QR codes. To install it, it’s good practice to create a virtual environment and install Segno into it using pip
from the command line.
00:26 On screen, you can see the commands you’ll need on mac OS or Linux.
00:44 And here’s what you’d need on Windows using Windows Terminal.
01:07
Once you’ve installed segno
, create a Python file named basic _qrcode.py
. To create a black and white QR code object that encodes some content, you’ll have to use the make_qr()
function.
01:20
This ensures that you are creating a full-size QR code, and the only mandatory argument you’ll need to pass is the information that you want to encode. Note that you can also use the make()
function to create a QR code object, but depending on the content you’re encoding, it might create a microQR code.
01:37
You’ll be looking at this later on in the course, so for the time being, stick with using make_qr()
.
01:45
QR codes are capable of handling a wide range of data, such as alphanumeric characters, symbols, and even URLs. To begin your journey of generating QR codes in Python, you’ll start by creating one that will encode the text “Hello, world.” First, you import segno
.
02:02 You store the encoded content as a variable
02:06
and use the save
method to save the QR code as an image. The save
method serializes the QR code into a file format of your choice as long as that format is supported.
02:21
When you apply save
to the variable you’ve created with the encoded content, you need to specify the file name, including an optional file path.
02:30
In this example, you’re saving the QR code image as a file named, basic _qrcode.png
in the same directory where you’ll be executing your code so you don’t specify a file path.
02:43
Once you’ve finished and saved the code in basic_qrcode.py
, you’ll need to run the Python script from the command line as seen.
02:54
If you look in the directory where the script is stored, you should now see a file named basic_qrcode.py
and opening it should show the image.
03:02 You may be able to do this from the command line as seen on screen.
03:10 You can now scan this with a phone camera. Most camera apps have a built-in QR code reader and should alert you to the presence of the information contained within.
03:19 One thing you may have noticed with the code you’ve generated so far is that it can be a little difficult to view or read because of its size, which is pretty small.
03:28 So in the next section of the course, you’ll take a look at how you can change the size of the QR codes that you are generating.
Become a Member to join the conversation.