How to read barcode using python
Step by step guide using OpenCV and pyzbar with code available in GitHub repository
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.
- Barcode of Wikipedia in CODE128 barcode format.
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.