Kodeclik Blog
Creating Your First QR Code with Python
You know those QR codes - square blobs of black and white that you are supposed to scan with your phone? And when you scan correctly, the bounding boxes hone in on the image and direct you to (typically) a website or so. This has become a modern, compact way for companies, restaurants, and even teachers to share links, Wi-Fi passwords, and information with you without typing long URLs.
For instance, the QR code below, when scanned, should take you to the Kodeclik website for you! Imagine putting something like this on your homework sheet, your science fair poster, or even a birthday invitation that links to a secret webpage.
How do we create such QR codes? It turns out you can do it very easily with Python! In this blogpost we are going to learn how to create them and what you can do with them once they are created. By the end, you will be able to generate your own QR codes from any text or URL you like.
What is a QR code?
Before we jump into code, let’s talk about what a QR code actually is. QR stands for “Quick Response.” QR codes were invented in the 1990s by a Japanese company originally to track car parts in factories more efficiently than regular barcodes.
A normal barcode is one-dimensional: it only stores information horizontally (those vertical lines you see on products). A QR code is two-dimensional: it stores information both horizontally and vertically in a grid of squares. That is why it can store much more data in a small space.
If you look closely at a QR code, you will see three big squares in three corners. These are “position markers” that help your phone’s camera figure out how the QR code is rotated. The rest of the small black and white squares encode the actual data, such as a URL, some text, a phone number, or other information. There is also built-in “error correction,” which means the QR code can still be read even if part of it is smudged, scratched, or covered by a small logo.
When you scan a QR code, your camera or QR scanner app detects those corner squares, straightens the image, reads the pattern of small squares, and decodes them back into text. If the text starts with something like https://, your phone knows it is a website and offers to open it in your browser.
Python program to create QR codes
Here’s a very bare bones program to create QR codes. This program below creates a QR code for the Kodeclik website but you can change it to any other site you like!
import qrcode
# website
site = "https://www.kodeclik.com"
# create the QR qrcode
QRpic = qrcode.make(site)
# Save the QRPic
QRpic.save("kodeclik.png")Let’s walk through what this code is doing.
The first line import qrcode tells Python that we want to use a separate module (a library) called qrcode. This module contains ready-made functions for building QR code images so we do not have to write all the low-level image processing logic ourselves.
Next, we create a variable called site and set it to the URL we want the QR code to encode. In this example it is "https://www.kodeclik.com", but you could change that to any site, such as your own blog, a YouTube video, or a Google Forms link.
The line QRpic = qrcode.make(site) is where the magic happens. The make function takes the text or URL you give it and generates a QR code object that represents the image. You can think of QRpic as an in-memory picture of your QR code.
Finally, QRpic.save("kodeclik.png") saves that image to a file named kodeclik.png in the same folder where your Python program is located. After running the program, you should see this file appear in your directory. You can open it like any other image and even print it or paste it into documents.
If you run the code, you will typically get an error like:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import qrcode
ModuleNotFoundError: No module named 'qrcode'This error message is Python’s way of telling you that it does not know what qrcode is. In other words, the qrcode module is not installed in your Python environment yet. Python is looking for a module named qrcode, cannot find it, and therefore stops the program with this error.
To fix this, we need to install the qrcode module before we can use it.
Installing the QR code module
The easiest way to install the module (if you are on a Mac or other Unix-type system like Linux) is to open your Terminal application and type:
pip install qrcodeIf you are on Microsoft Windows, the idea is the same, but you will usually open Command Prompt or PowerShell instead of Terminal. You can search for “Command Prompt” in the Start menu, open it, and then type the installation command.
Running your program
After installing the module, you can run your program again. This time, instead of an error, the program should finish quietly and you should see a new file called kodeclik.png appear in your folder. Open that image and you will see a QR code.
Now grab your phone and open the camera app (or a QR code scanning app, depending on your device). Point it at the QR code on your screen. If everything works correctly, your phone should recognize the QR code and offer to open the Kodeclik website in your browser.
Next, try changing the value of the site variable to something else. For example, you could set it to a school website, a favorite blog, or even to plain text like "Hello from Python!". Run the program again and notice that a new QR code image is created.
But note that If you use the same filename (kodeclik.png), it will overwrite the old one with the new QR code. If you use a different filename, you can keep multiple QR codes side by side. This is a fun way to experiment with how QR codes look and behave for different kinds of text.
More options to try
The code shown above is the simplest segment you can write to explore the QR code generator in Python. A more elaborate piece of code is given below:
import qrcode
# website
site = "https://www.kodeclik.com"
# create the QR qrcode with full options
qr = qrcode.QRCode(
version=1, # controls overall size: 1 is smallest, higher = bigger code
error_correction=qrcode.constants.ERROR_CORRECT_H, # L, M, Q, or H
box_size=10, # pixel size of each square
border=4 # thickness of white border (in boxes)
)
qr.add_data(site)
qr.make(fit=True)
# Dark purple on soft lavender
QRpic = qr.make_image(fill_color="#3b155a", back_color="#f3e8ff")
# save the image to a file
QRpic.save("kodeclik.png")This code now creates a configurable QRCode object, adds data and “builds” the QR code, then converts it into an image with make_image, and finally saves it into a file. This program is the one which generates the "colorful" QR code shown earlier.
Take a look at all the options we have used here:
- version: This controls the overall size (data capacity) of the QR code. QR codes come in standardized “versions” numbered 1 to 40. version=1 is the smallest (21x21 modules). Higher versions add more rows/columns and can store more data, but the image becomes more dense and larger. Using version=None (or fit=True later) lets the library automatically choose the smallest version that fits your data.
- error_correction: This parameter controls how much of the QR code can be damaged, covered, or noisy while still being scannable. Higher error correction means more redundancy (and a denser/bigger code). Common options are qrcode.constants.ERROR_CORRECT_M – Medium (~15% of the code can be restored) and qrcode.constants.ERROR_CORRECT_H – High (~30%, if you plan to overlay a logo or expect wear/printing issues).
- box_size: This parameter sets the pixel size of each individual square (module) in the QR code. A larger box_size means a bigger image on screen or paper while a smaller box_size might be more compact but harder to scan if printed very small.
- border: This parameter sets the width of the quiet zone around the QR code, measured in modules (not pixels). In other words, this is the white margin surrounding the black pattern. A value of 4 is the commonly recommended minimum for reliable scanning. Increasing it adds more white space around the code, which can improve readability against busy backgrounds.
- fill_color: This parameter is used in the qr.make_image step. It sets the color of the dark “on” modules (traditionally black). You can give the RGB code as we have given above. Make sure there is good contrast with the background.
- back_color: This parameter is also used in the qr.make_image step and sets the color of the background (the “off” modules), traditionally white. Again, usually kept light or white to maximize contrast with fill_color.
Summary
In summary, a QR code is simply a clever way to pack text (like a URL) into a little square picture that machines can read quickly.
We saw how to use Python’s qrcode module to generate a QR code from a URL, how to save it as an image file, and how to fix the “ModuleNotFoundError” by installing the required module with pip. Once your setup is working, generating QR codes becomes just a few lines of code.
From here, you can customize your QR codes with different data, colors, and sizes, or even generate whole batches of codes for projects and classroom activities. QR codes may look like mysterious patterns at first, but with a little Python, you are now in control of creating them yourself.
Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.