📷 How to Read QR Code from Image Using Python (pyzbar + PIL)
Want to scan and decode QR codes using Python? This tutorial shows how to extract QR code data from an image using the pyzbar and Pillow (PIL) libraries.
This method is widely used in:
- 📱 QR-based authentication systems
- 📄 Document verification
- 🔗 URL & data extraction tools
- 🤖 AI & automation workflows
✅ What This Python Script Does
The script:
- 🖼 Opens an image containing a QR code
- 🔍 Detects QR codes inside the image
- 📄 Decodes and prints QR content
- ❌ Displays a message if no QR is found
📦 Step 1: Install Required Libraries
Install pyzbar and Pillow using pip:
pip install pyzbar pillow
Note: On Windows, you may need to install the ZBar dependency separately.
🧠 Step 2: Python Code to Decode QR Code
Here is the complete Python script to read QR data from an image:
from pyzbar.pyzbar import decode
from PIL import Image
# Load image
img = Image.open("image.png")
# Decode QR codes
results = decode(img)
if results:
for qr in results:
print("QR found:", qr.data.decode())
else:
print("No QR found")
📌 Output Example
If a QR code is detected:
QR found: https://example.com
If no QR code exists in the image:
No QR found
🛠 How It Works (Simple Explanation)
Image.open()→ Opens the image filedecode()→ Scans the image for QR codesqr.data.decode()→ Converts QR data to readable text
🚀 Use Cases
- ✔ QR login verification
- ✔ Scanning payment QR codes
- ✔ Extracting URLs from images
- ✔ AI image-to-text pipelines
⚠ Common Errors & Fixes
- Error:
ImportError: Unable to find zbar shared library
Fix: Install ZBar system dependency - Error: No QR detected
Fix: Ensure image is clear and QR is not blurred
🔚 Conclusion
This simple Python QR scanner is fast, lightweight, and ideal for automation, AI tools, and real-world applications. You can easily extend it for:
- 📂 Batch image processing
- 🌐 Web apps (Flask / FastAPI)
- 🤖 AI and RAG pipelines
Happy Coding! 🚀
