How to Generate and Read QR Codes with Python: A Step‑by‑Step Guide
Learn what QR codes are, how to install the Python qrcode library, create simple and advanced QR codes with customizable parameters, and decode them using OpenCV, all with clear code examples and practical tips for safe usage.
What is a QR code?
QR codes are machine‑readable two‑dimensional barcodes that can store various kinds of information. They were invented in 1994 by Masahiro Hara of Denso Wave to track automotive parts and became popular in the late 2010s as smartphones added built‑in scanners. Today they are used for payments, menus, Wi‑Fi passwords, product details, and more.
In this article we will learn how to generate and read QR codes using Python.
Generating QR Codes
First, install the qrcode package: pip install qrcode Simple QR code example:
# Import library
import qrcode
# Generate QR code
img = qrcode.make('Hello World')
img.save('hello.png')Note: Do not scan random QR codes with your phone, as they may contain malicious links or code.
Advanced QR Codes
The QRCode object allows customization via several parameters:
version : Controls the size of the code (1–40). Version 1 creates a 21×21 matrix.
error_correction : Determines error‑correction level (L 7%, M 15%, Q 25%, H 30%).
box_size : Number of pixels for each box.
border : Thickness of the border (default 4).
Key methods of the QRCode object:
add_data : Add the data to encode.
make : Build the QR matrix; set fit=True to let the library choose the optimal version.
make_image : Render the QR code as an image; fill_color and back_color can customize colors.
Advanced example (creates a red QR code on a black background pointing to a Medium profile):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data("https://abhijithchandradas.medium.com/")
qr.make(fit=True)
img = qr.make_image(fill_color="red", back_color="black")
img.save("medium.png")Reading QR Codes
Install OpenCV if it is not already present: pip install cv2 Use OpenCV’s QRCodeDetector to decode a QR image:
import cv2
img = cv2.imread("medium.png")
det = cv2.QRCodeDetector()
val, pts, st_code = det.detectAndDecode(img)
print(val) # prints the encoded URLThe detectAndDecode function returns the decoded text, the corner points of the QR code, and a binarized version of the QR image.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
