How to read barcode using python

Step by step guide using OpenCV and pyzbar with code available in GitHub repository

Abhijith Chandradas
3 min readAug 25, 2024

Prerequisites

Reading barcode in python requires the following libraries to be installed.

Pyzbar is used to decode the data from barcode image.
OpenCV is an image processing package used to read and view the barcodes images.

These libraries can be installed as below.

pip install opencv-python
pip install pyzbar

Import Libraries

The libraries mentioned above can be imported as below.

# Import libraries 
import cv2
from pyzbar.pyzbar import decode

Bar Code Images

The following two images will be used in this article. These images and a few more are available in images folder of this GitHub repo.

  1. Barcode of Wikipedia in CODE128 barcode format.
Wikipedia Barcode

2. Barcode of a product clicked using mobile camera in jpeg format. Note that the image contains more information in addition to the barcode and also the barcode is not perfectly aligned.

--

--

Abhijith Chandradas

Data Analyst | Hacker | Financial Analyst | Freelancer | IIM MBA | Opensource | Democratize Knowledge | https://www.youtube.com/channel/UCLpBd4gzfIBXm2BPpdHOWdQ