Decode QR Codes in Python with Zxing: A Complete Step-by-Step Tutorial

This guide walks through setting up Python’s Zxing library to decode QR code images, covering installation, handling 64‑bit compatibility issues, a complete script that creates temporary files, reads barcodes, logs results, and cleans up, enabling quick integration into your projects.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Decode QR Codes in Python with Zxing: A Complete Step-by-Step Tutorial

Zxing for QR Code Recognition

The author initially tried ZBar but encountered 32‑bit DLL errors on a 64‑bit system and learned that ZBar cannot handle tilted or locate barcodes. Consequently, Zxing was chosen as an alternative.

Installation

Install the Python wrapper for Zxing via pip:

pip install zxing

Complete Python Script

The following script demonstrates how to decode a single‑image QR code using Zxing. It creates a temporary JPEG file, decodes the barcode, logs the outcome, and removes the temporary file.

#encoding:utf8
"""
Single‑image QR code must be processed individually; multiple images may cause issues.
This example supports only JPG format.
"""
import os
import logging
from PIL import Image
import zxing
import random

logger = logging.getLogger(__name__)
if not logger.handlers:
    logging.basicConfig(level=logging.INFO)
DEBUG = (logging.getLevelName(logger.getEffectiveLevel()) == 'DEBUG')

def ocr_qrcode_zxing(filename):
    # Generate a temporary file in the current directory
    img = Image.open(filename)
    ran = int(random.random() * 100000)
    temp_name = f"{os.path.basename(filename).split('.')[0]}{ran}.jpg"
    img.save(temp_name)
    zx = zxing.BarCodeReader()
    data = ''
    zxdata = zx.decode(temp_name)
    # Delete the temporary file
    os.remove(temp_name)
    if zxdata:
        logger.debug(f"zxing recognized QR code: {filename}, content: {zxdata}")
        data = zxdata
    else:
        logger.error(f"Error recognizing QR code with zxing: {filename}")
        img.save(f"{filename}-zxing.jpg")
    return data

if __name__ == '__main__':
    # Path to the QR code image
    filename = r'/Users/xxx/Downloads//3333.jpg'
    # Perform QR code recognition using Zxing
    ltext = ocr_qrcode_zxing(filename)
    logger.info(f"[{filename}] Zxing QR code result: [{ltext}]!!!")
    print(ltext)

The script logs detailed debug information when the logging level is set to DEBUG, and it provides error handling by saving a copy of the problematic image.

Note: The article originally included a promotional QR code image for additional resources, which has been removed as it does not pertain to the technical content.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonImage ProcessingTutorialQR codeZXingbarcode
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.