Airfrog editorial team ·
Read QR Codes with Python: Text and Coordinates
How to read QR codes from PNG, JPEG, and image bytes with Airfrog's Python API. Multiple codes, coordinates, invalid input, and how to obtain a build.
On this page
Read every QR code from image bytes with the Python API
To read QR codes from a photo with Airfrog, pass the file contents to QRDecoder().decode_all(image_bytes). It returns a list of (text, corners) pairs: the text and corners for each decoded code. Python calls the Rust core and waits for one image to finish processing.
from pathlib import Path
from qr_python import QRDecoder
decoder = QRDecoder()
try:
results = decoder.decode_all(Path("frame.png").read_bytes())
except (OSError, ValueError) as error:
print("Could not process image:", error)
else:
for text, corners in results:
print(text, corners)
if not results:
print("No QR codes were decoded; check the source image") decode returns only the first decoded result or None; use decode_all when an image contains multiple labels. Do not reduce the list to one value if missing a second QR could affect inventory or order checks.
PNG and JPEG: pass the file contents
To read a QR code from a PNG or JPEG, use Path("frame.jpg").read_bytes(). The decoder opens the image from the bytes you pass in. A path, URL, or base64 string is not valid input: read the file first, and decode base64 in your application.
Keep the original resolution. JPEG recompression, sharing through a messaging app, and downscaling can erase QR module edges. If the camera stores orientation in EXIF, normalize the image and its coordinates before passing it to the decoder; the example above does not do this.
Coordinates and identical text in different locations
corners contains the detected QR code's corners as integer image coordinates. Use them to crop a region or match a code to an expected label by position. Do not assume the first result is the leftmost or highest: list order does not define item order.
If two labels contain the same text, do not turn the results into a set of strings; that would merge separate spatial objects. Check the text together with its position and the expected number of codes. Read more about multiple QR codes with identical content.
An empty result and a file error are different
Python handles file read errors, while the binding reports image decoding errors as ValueError. An empty list means no QR codes were decoded on this attempt; it does not prove that the frame contains none. For critical operations, request another capture or a manual check instead of automatically confirming that an item is missing.
How to get the Rust-based Python library
The examples import qr_python. Request a build for your Python version, operating system, and architecture; this page does not publish a universal PyPI install command. If you work from source, build the Python bindings with maturin.
Before integrating, verify the import and a test image in the target environment. Platform availability and access terms are covered in the integration guide.
Server-side processing and time budgets
Each call processes a frame synchronously. If you call it from an HTTP service, your application should limit input size, concurrent tasks, and wait time. For heavy frames, consider processing them outside the HTTP request path. These controls belong to your service; the Python API does not provide a ready-made task queue.
Processing time depends on image resolution and complexity. Measure slow attempts as well as the average on your target server; see how to prepare an SDK test with your own data.
From file to result: an image processing workflow
Suppose a camera saves a photo of a box with three labels. Your application reads the file, passes its contents to decode_all, and gets a list of texts with coordinates. Your own logic then matches labels to the order, marks them on the photo, and decides whether the operation can be confirmed.
Track three outcomes separately: whether the image opened, how many QR codes were decoded, and whether your application accepted the decoded values. This keeps a file error from becoming “item not found” and prevents two decoded labels out of three from looking like a complete result.
| Result | What to do in your application |
|---|---|
| List of texts and corners | Check the content, positions, and expected object count. |
| Empty list | Record “not decoded” and follow your retry policy. |
| ValueError | Check the input file and format; do not treat the attempt as proof that no QR is present. |
| OSError from read_bytes | Check the path, file access, and whether the camera has finished writing the file. |
When processing a folder, track each file separately. Create the decoder before the loop and pass images one at a time; verify concurrency settings on the target build. Include the filename, result count, processing time, and error reason in the report so you can repeat a specific attempt.
Python QR code reader FAQ
Can I pass an OpenCV image or NumPy array?
The method shown accepts encoded image bytes. An OpenCV pixel array has a different format, so convert it in memory to a supported image file in your existing pipeline. If your application needs this conversion, include its time in your measurements.
How do I map coordinates back to the original photo?
If you pass the original file, the coordinates refer to that image. After cropping, add the crop offset; after resizing, scale each axis. Keep the transformation parameters with the result or the box will appear in the wrong place when you display the original.
Should I open a link encoded in a QR code?
Reading the code only requires getting its text. Opening a URL, checking its domain, and making network requests are application logic. For a warehouse label, validate the expected identifier format before following any link.