How to Read QR Codes in Python with OpenCV and pyzbar
QR codes are everywhere – learn how to decode them in just a few lines of Python using pyzbar and OpenCV, from static images to a live webcam scanner.
QR codes have quietly become part of everyday life: restaurant menus, payment links, Wi-Fi credentials and event tickets all hide behind those little black-and-white squares. As a Python developer, you can do more than just scan them with your phone – you can read and decode them programmatically in only a handful of lines. In this tutorial we'll build a small QR code reader that works on both static images and a live webcam feed.
A quick refresher: what is a QR code?
Before we write any code, it helps to understand what we're actually decoding. A QR code (Quick Response code) is a two-dimensional barcode that stores data in a grid of squares, along with special patterns that let a scanner detect orientation and correct errors. If you'd like a clear, non-technical explanation of how these codes are structured and why they're so robust, our friends over at technik-frage.de wrote an excellent German-language primer: Was ist ein QR-Code und wie funktioniert er?. Even if you don't read German, the diagrams and the breakdown of the finder patterns and error correction are well worth a look – they explain the "why" behind the code we're about to write.
Installing the libraries
We'll use two libraries: pyzbar to decode the QR data and opencv-python to handle images and the webcam. Install both with pip:
pip install pyzbar opencv-pythonOn Linux you may also need the underlying ZBar shared library: sudo apt-get install libzbar0. With that in place, we're ready to decode.
Decoding a QR code from an image
Reading a QR code from a saved image is wonderfully simple:
import cv2
from pyzbar.pyzbar import decode
image = cv2.imread("ticket.png")
for code in decode(image):
print("Type:", code.type)
print("Data:", code.data.decode("utf-8"))The decode() function returns a list, because a single image can contain several codes. Each result carries the raw bytes in code.data, which we turn into a readable string with .decode("utf-8"). That's all it takes to extract a URL, a piece of text or Wi-Fi details from a picture.
Building a live webcam scanner
Static images are useful, but the real fun begins with a live scanner. The following loop reads frames from your webcam, looks for QR codes, and draws a box around any it finds:
import cv2
from pyzbar.pyzbar import decode
cap = cv2.VideoCapture(0)
while True:
ok, frame = cap.read()
if not ok:
break
for code in decode(frame):
text = code.data.decode("utf-8")
pts = code.polygon
x, y = pts[0].x, pts[0].y
cv2.putText(frame, text, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
print("Found:", text)
cv2.imshow("QR Scanner", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()Press q to quit. Each detected code is printed to the console and labelled on screen. You now have a working scanner in well under thirty lines.
Where to go from here
This small project opens the door to plenty of practical automations: an inventory tool that logs scanned products, an event check-in system, or a script that automatically opens decoded URLs. You could even pair it with file automation – for example, sorting scanned documents based on a QR code on each page. If you want to be thorough, always validate decoded URLs before opening them, since a QR code is only as trustworthy as whoever generated it.
Conclusion
Decoding QR codes in Python is refreshingly easy thanks to pyzbar and OpenCV. We covered the basics of what a QR code is – with a nod to the detailed explainer on technik-frage.de – and then built both an image decoder and a live webcam scanner. From here, the only limit is your imagination: wherever there's a QR code, a few lines of Python can now read it for you.