Scan Barcodes & QR Codes in Python (pyzbar Tutorial)

Опубликовано: 31 Июль 2026
на канале: Generalist Programmer
49
0

▶ Try this lesson free: https://generalistprogrammer.com/tuto...

Five lines of Python can read any barcode or QR code — from a saved image or straight from your
live webcam — and hand you back the decoded text. No cloud service, no paid API, no machine
learning to train. This is the full pyzbar walkthrough: install (and the one native-library
gotcha that trips everyone up), decode from an image, decode from an OpenCV frame, and a complete
live webcam scanner that draws a box around each code and prints its value in real time.

What's covered:
Install — pip is only half of it. pip install pyzbar gives you the Python wrapper, but pyzbar
wraps a native C library called zbar that pip does NOT bundle. On Windows you also need the
Visual C++ 2013 Redistributable (vcredist_x64) or you hit
"FileNotFoundError: Could not find module libzbar-64.dll (or one of its dependencies)". On
macOS: brew install zbar. On Debian/Ubuntu: sudo apt-get install libzbar0.
Read from an image — the import is "from pyzbar.pyzbar import decode" (note the DOUBLED name).
decode(Image.open("qr.png")) with Pillow returns a LIST of Decoded objects. Each has .data
(raw BYTES — you must call .decode("utf-8") to get a string), .type (an uppercase string like
"QRCODE", "EAN13", "CODE128"), .rect (Rect with left, top, width, height) and .polygon (a list
of Point x, y corners). The .data-is-bytes gotcha catches everyone.
Decode an OpenCV frame — no Pillow needed. pyzbar accepts a NumPy array directly, so
img = cv2.imread("qr.png"); decode(img) just works, on colour BGR or grayscale. The still-image
code is identical to the per-frame code.
Live webcam scanner — cap = cv2.VideoCapture(0), read frames, decode(frame), draw the box with
cv2.rectangle from barcode.rect (top-left = left, top; bottom-right = left+width, top+height),
cv2.putText the decoded text, cv2.imshow, quit on 'q'. Under 20 lines.
Barcode types + filtering — pyzbar/zbar reads QR codes, EAN-13/EAN-8, UPC-A/UPC-E, Code 128,
Code 39, ITF and more. Restrict to one with symbols=[ZBarSymbol.QRCODE]
("from pyzbar.pyzbar import ZBarSymbol") for speed and fewer false reads.
Project ideas — inventory scanner, attendance via QR, ticket validation, WiFi-QR reader.

Two gotchas to remember: the import is pyzbar.pyzbar (doubled), and .data is bytes so always
.decode("utf-8").

Free written guide — the Python questions that come up most, great everyday practice:
https://generalistprogrammer.com/tuto...

Want to level up faster? Python Power Pack — 50 real-world snippets, idioms, and interview prep,
19 dollars, optional:
https://generalistprogrammer.gumroad....

— Chapters —
0:00 5 lines to read any barcode or QR code
0:49 Install: pip + the native zbar library (DLL gotcha)
2:12 Decode from an image (.data is bytes)
3:39 Decode an OpenCV frame (NumPy array)
4:51 Live webcam scanner
6:17 Barcode types + ZBarSymbol filter
7:39 Project ideas
8:40 Recap + next step

— Title variants (A/B testing) —
1. Scan Barcodes & QR Codes in Python (pyzbar Tutorial)
2. Python Barcode & QR Code Scanner in 5 Lines (pyzbar + OpenCV)
3. Read Any QR Code or Barcode in Python with pyzbar (Full Guide)

#python #pyzbar #qrcode