使用Python读取pyqrcode生成的二维码图片返回空结果问题排查
Got it, let's tackle this QR code reading issue you're facing. Since your generated QR works perfectly on mobile but returns an empty list when reading it in Python, and you want to avoid zbar/OpenCV dependencies, here are actionable solutions:
1. Switch to a pure-Python, zbar-free QR reading library: qreader
This library uses an ONNX-based AI model for QR detection and decoding, so it doesn't rely on tricky native dependencies like zbar or OpenCV. It's easy to install and has great accuracy.
First, install the required packages:
pip install qreader pillow onnxruntime
Then use this code to read your QR.png file:
from qreader import QReader from PIL import Image # Initialize the QR reader qr_reader = QReader() # Load your QR code image qr_image = Image.open("QR.png") # Detect and decode the QR content decoded_content = qr_reader.detect_and_decode(image=qr_image) print("Decoded QR content:", decoded_content)
This should return the correct data since it handles standard QR codes reliably, even those generated by pyqrcode.
2. Rule out basic reading-side issues
Before jumping to library changes, double-check these quick fixes:
- Confirm the image path is correct (no typos, absolute paths work better if you're unsure)
- Verify the
QR.pngfile isn't corrupted: open it with PIL to ensure it loads properly - Avoid any unintended image modifications (like resizing or compressing) before reading—this can warp the QR pattern and break detection
3. Validate your QR generation step
Even though mobile readers work, it's worth confirming the generated QR holds the correct data. Add this line right after generating your QR code to verify:
print("Encoded data in QR:", myQR.data)
This ensures your data variable was correctly encoded into the QR code, ruling out any encoding mismatches in the generation process.
内容的提问来源于stack exchange,提问作者Ashy

