Decoding Barcodes in Python (using pyzbar)

In this blog, we will demystify the mystery of Decoding Barcodes from images in Python. We will be making use of pyzbar module in achieving the same. Decoding Barcodes is easy in Python language, you just need to follow the course of this blog.

Installation and Loading of Dependencies Required


The pyzbar module is capable of reading and decoding one-dimensional barcodes and QR codes. The features of the module are:

  • Easy implementation in Python
  • Works with PIL / Pillow images, OpenCV / numpy ndarrays, and raw bytes
  • Decodes locations of barcodes


PIL or Pillow is an Image Processing library that provides us with extensive methods of operations that can be performed on images. We will use it for opening the images.

We can install these packages easily using the pip module.

pip install pyzbar
pip install pillow

After the installation part is done, we will be importing:

  • decode from pyzbar.pyzbar module
  • Image from PIL module
from pyzbar.pyzbar import decode
from PIL import Image

Decoding the Barcodes!

To decode the barcodes, we will open the image of the Barcode using the Image module, and then we will pass the image as a parameter inside the decode() method.

The decode() method returns a list of namedtuple called Decoded. Every decoded tuple consists of the following attributes:

  • data — The decoded string in bytes. We have to further decode it using utf8 to get a string.
  • type — This attribute holds the type of barcode decoded.
  • rect — A Rect object which represents the captured localization area.
  • polygon — A list of Point instances that represent the barcode.

Input Image:

Python Code:

from pyzbar.pyzbar import decode
from PIL import Image
info = decode('PATH\\NAME OF IMAGE'))


[Decoded(data=b'', type='CODE128', rect=Rect(left=34, top=11, width=773, height=177), polygon=[Point(x=34, y=11), Point(x=34, y=187), Point(x=807, y=188), Point(x=807, y=12)])]

To get the information (data) as a string, we can loop over the Decoded Tuple in the manner mentioned below:

from pyzbar.pyzbar import decode
from PIL import Image
info = decode('PATH\\NAME OF IMAGE'))
for i in info:


We loop over info because there can be more than one Decoded Tuples in the list. So the loop extracts data from all of them in this way.

So, that was all about demystifying Decoding Barcodes. Thank you for devoting your valuable time in reading this blog. You may check out these other articles as well:

Leave a Reply

Your email address will not be published. Required fields are marked *