Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
428: Building a QR Code Generator
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.QRCodeclass rather than themake()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.
There are no comments for now.