Skip to Content
Course content

428: Building a QR Code Generator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When people first start thinking about building a QR code generator, they often fall into the trap of thinking they need to be an expert in image processing or coordinate geometry. I've seen many developers try to approach this by thinking, "I just need to create a 2D array of black and white squares and map my string to those pixels."

The "Manual Matrix" Myth

If you try to write the logic that converts a string like https://github.com/yourprofile into a grid of pixels manually, you'll quickly realize you're not just dealing with a simple map. QR codes use Reed-Solomon error correction, which means they add redundant data so the code still works even if the print is smudged or partially torn. If you try to "hand-draw" the matrix without this mathematical foundation, your generated image might look like a QR code, but no scanner in the world will be able to read it.

Letting the qrcode Library Handle the Math

In the real world, we don't reinvent the wheel for standard protocols. We use the qrcode library because it handles the encoding and the error correction logic for us. To get started, you'll need both qrcode and pillow (the Python Imaging Library), as qrcode uses Pillow to actually render the matrix into an image file.

import qrcode

# The simplest way to do this is using the shortcut function
img = qrcode.make("https://www.python.org")
img.save("python_qr.png")

That's it. But as a software engineer, you'll rarely be satisfied with the defaults. Usually, your stakeholders will want the QR code to look a certain way or be robust enough to survive being printed on a grainy cardboard box.

Fine-Tuning Density and Error Recovery

To get more control, we move away from the make() shortcut and use the QRCode class. This allows us to tweak the version (which controls the size of the QR code) and the error_correction level.

I personally always recommend ERROR_CORRECT_H (High) if the code is going to be printed on physical media. It allows the code to be recovered even if up to 30% of the image is obscured. It makes the QR code denser, but it's worth the trade-off for reliability.

import qrcode

qr = qrcode.QRCode(
    version=1, # 1 is the smallest; higher numbers increase the grid size
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=10, # How many pixels each "box" of the QR code is
    border=4,    # The thickness of the white border
)

qr.add_data("https://developer.mozilla.org")
qr.make(fit=True)

# You can even change the colors here
img = qr.make_image(fill_color="darkblue", back_color="white")
img.save("dev_docs_qr.png")

Note the fit=True argument in make(). I use this because it tells the library to automatically scale up the version if the data you're adding is too large for the initial version you specified. It saves you from having to manually calculate exactly how many characters fit into a Version 1 code.




📋 Practical Task

Build a Customizable Wi-Fi Connection QR Generator

Standard QR codes for Wi-Fi networks follow a very specific string format: WIFI:S:NetworkName;T:WPA;P:Password;;. Your task is to write a script that prompts the user for their SSID (Network Name) and Password, formats that string correctly, and generates a high-error-correction QR code saved as wifi_access.png.

  • Use the qrcode.QRCode class rather than the make() shortcut.
  • Set the error correction to ERROR_CORRECT_H.
  • Ensure the final image uses a custom fill color (e.g., "navy" or "darkgreen") to make it look professional.
  • The script should handle the string formatting automatically so the user only has to provide the raw network details.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.