Skip to content

How to Fix Python Pyzbar Barcode Decode Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Python Pyzbar Barcode Decode Error. We cover key concepts, practical examples, and best practices.

Pyzbar returns an empty list or raises ImportError when the ZBar library is not installed, the image format is not supported, or the barcode is too distorted to read.

The Wrong Way

from pyzbar.pyzbar import decode
from PIL import Image

img = Image.open("qr_code.png")
results = decode(img)
if results:
    print(results[0].data)

If ZBar is not installed on the system:

ImportError: Unable to find zbar shared library

The Right Way

from pyzbar.pyzbar import decode
from PIL import Image
import numpy as np

# Install ZBar: sudo apt install zbar-tools (Linux)
# or use prebuilt wheels

img = Image.open("qr_code.png")
# Convert to grayscale for better detection
img = img.convert("L")
results = decode(img)

if results:
    for result in results:
        print(f"Data: {result.data.decode('utf-8')}")
        print(f"Type: {result.type}")
else:
    print("No barcode found")

Output:

Data: https://dodatech.com
Type: QRCODE

Prevention Tips

  • Install system ZBar library: apt install zbar-tools or brew install zbar.
  • Convert images to grayscale before decoding.
  • Ensure barcode is well-lit and in focus.
  • Use pil backend for direct PIL image support.
  • Try different image orientations if decoding fails.

Common Mistakes with pyzbar decode

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What barcode formats does Pyzbar support?

PyZbar supports QR Code, Code 128, Code 39, EAN-13, EAN-8, UPC-A, UPC-E, PDF417, Data Matrix, and Aztec. The result.type field indicates which format was detected.

Why does Pyzbar fail to decode a QR code on a curved surface?

Distorted barcodes are hard to decode. Preprocess the image: convert to grayscale, apply Gaussian blur, and threshold. Try cv2.adaptiveThreshold() in OpenCV before passing to Pyzbar.

How do I decode barcodes from a video stream?

Extract frames from cv2.VideoCapture(), convert to PIL Image, and call decode(). Process every Nth frame for performance. Use threading to avoid frame drops.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro