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.
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
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:
- Load the image containing the barcode or QR code.
- Use the decoding functionality provided by the
pyzbarlibrary to extract the data from the image. - 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:
- We import the necessary libraries:
decodefrompyzbar.pyzbarfor decoding barcodes and QR codes, andImagefromPILfor working with images. - We define a function called
scan_codesthat takes an image path as a parameter. Inside the function:
- We open the image using the
Image.openmethod from PIL. - We use the
decodefunction frompyzbar.pyzbarto decode the barcodes and QR codes from the image. Thedecodefunction 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_datalist.
-
We provide the path to the barcode or QR code image in the
image_pathvariable. -
We call the
scan_codesfunction with theimage_pathand store the returned data in thecode_datavariable. -
If
code_datais notNone, 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