← Back to list

Building an Effective Barcode Scanner in Python with pyzbar

Detect multiple types of barcodes such as CODBAR, CODE39, CODE93, CODE128, DATABAR, ean13, I25 and QR codes.

Rahul Bhat in Towards Dev · 2023-05-25 11:57 · 10 claps · 2.8 min read
#barcode-reader #barcode-scanner #pyzbar #qr-code #barcode
Open on Medium ↗

Building an Effective Barcode Scanner in Python with pyzbar

Detect multiple types of barcodes such as CODBAR, CODE39, CODE93, CODE128, DATABAR, ean13, I25 and QR codes.

Photo by José León on Unsplash

Photo by José León on Unsplash

Introduction

When it comes to barcode detection, many existing blogs and programs often encounter issues or fail to function properly. Problems may arise during library installation, or the detection function may struggle to accurately identify barcodes. However, after extensive research, I discovered a barcode detection library called pyzbar that effectively detects multiple barcode types simultaneously.

This library is easy to install using pip. Although the function I will demonstrate in this blog can detect various barcode types, it is important to note that there are certain types it may not identify. To ensure accuracy, I have extensively tested the function with different barcode types.

What types of barcodes would be able to detect?

CODBAR, CODE39, CODE93, CODE128, DATABAR, ean13, I25 and QR barcodes.

What type of barcodes would not be able to detect?

AZTEC, MSI PLESSEY and PDF417.

Prerequisites

Before we begin, make sure you have the following installed:

  • Python (3.x recommended)
  • pillow library (pip install Pillow)
  • pyzbar library (pip install pyzbar)
pip install pyzbar Pillow

Scanning Barcodes and QR Codes

First, let’s understand the basic steps involved in scanning barcodes and QR codes using Python:

  1. Load the image containing the barcode or QR code.
  2. Use the decoding functionality provided by the pyzbar library to extract the data from the image.
  3. Iterate over the decoded data and retrieve the barcode type and data for each code.

Implementing the Barcode Scanner

from pyzbar.pyzbar import decode
from PIL import Image

def scan_codes(image_path):
    # Load the image
    with open(image_path, 'rb') as image_file:
        image = Image.open(image_file)

        # Decode barcodes and QR codes
        decoded_data = decode(image)

        if decoded_data:
            code_data = []
            for data in decoded_data:
                barcode_type = data.type
                barcode_data = data.data.decode('utf-8')
                code_data.append((barcode_type, barcode_data))

            return code_data

    return None

image_path = 'barcode_pic.jpg'  # Replace with your barcode/QR code image path

code_data = scan_codes(image_path)

if code_data is not None:
    print('Scanned Codes:')
    for barcode_type, barcode_data in code_data:
        print('Barcode Type:', barcode_type)
        print('Barcode Data:', barcode_data)
else:
    print('No barcodes or QR codes found in the image.')

Output

Scanned Codes:
Barcode Type: CODE39
Barcode Data: ABC 123

Understand the code

Let’s break down the code step by step:

  1. We import the necessary libraries: decode from pyzbar.pyzbar for decoding barcodes and QR codes, and Image from PIL for working with images.
  2. We define a function called scan_codes that takes an image path as a parameter. Inside the function:
  • We open the image using the Image.open method from PIL.
  • We use the decode function from pyzbar.pyzbar to decode the barcodes and QR codes from the image. The decode function returns a list of decoded data.
  • If decoded data is found, we iterate over it and extract the barcode type and data. We store them in a list of tuples, code_data.
  • Finally, we return the code_data list.
  1. We provide the path to the barcode or QR code image in the image_path variable.

  2. We call the scan_codes function with the image_path and store the returned data in the code_data variable.

  3. If code_data is not None, we iterate over it and print the barcode type and data for each code. Otherwise, we print a message indicating.

Conclusion

Conclusion: In conclusion, while many barcode detection blogs and programs may encounter issues or fail to deliver accurate results, the pyzbar library offers a reliable solution for barcode detection in Python. By implementing the demonstrated barcode scanner function, you can detect various barcode types, including CODBAR, CODE39, CODE93, CODE128, DATABAR, ean13, I25, and QR codes. However, please note that the pyzbar library may not be able to detect AZTEC, MSI PLESSEY, and PDF417 barcodes.

By incorporating this barcode scanner into your Python applications, you can streamline processes and improve efficiency in tasks such as inventory management or retail solutions. Remember to use clear and well-focused barcode images for optimal results. If you require detection of unsupported barcode types, consider exploring alternative libraries that cater to your specific needs.

Thank you for reading, and I hope you find this tutorial helpful!


메타데이터
post_id
f186a2b9dcfc
slug
building-an-effective-barcode-scanner-in-python-with-pyzbar-f186a2b9dcfc
url
https://towardsdev.com/building-an-effective-barcode-scanner-in-python-with-pyzbar-f186a2b9dcfc
canonical_url
https://towardsdev.com/building-an-effective-barcode-scanner-in-python-with-pyzbar-f186a2b9dcfc
author_url
https://medium.com/@ra-bhat2002
status
ok
fetched_at
2026-07-25 17:51:23