Pyzbar – Python pyzbar Module for Decoding Barcodes

pyzbar module:

Pyzbar: The pyzbar module can read and decode one-dimensional barcodes as well as QR codes. The module’s features are as follows:

  • Simple Python implementation
  • Uses PIL / Pillow pictures, OpenCV / numpy ndarrays, and raw bytes
  • Locations of barcodes are decoded.

Installation of module:

pip install pyzbar

Output:

Collecting pyzbar
Downloading pyzbar-0.1.8-py2.py3-none-any.whl (28 kB)
Installing collected packages: pyzbar
Successfully installed pyzbar-0.1.8

pyzbar Module for Decoding Barcodes in Python

Let us take the below barcode as an example. Our goal is to retrieve the information contained within the barcode.

sample barcode image

To Get Information from the Barcode

barcode_informatn = decode(gvn_barimag)

To acquire information from an image of a barcode, use the decode function, which takes the image object as an argument.

However, the information stored in the barcode_informatn variable is presented in the block below. As you can see, the information acquired is incredibly jumbled, and nothing can be decrypted from it.

To the Display the Barcode Information

We will use the following code block to display only the data from the barcode image and ignore the rest of the useless information from the variable.

for k in barcode_informatn:
    print(k.data.decode("utf-8"))

Approach:

  • Import decode function from pyzbar module using the import keyword.
  • Import Image function from PIL module using the import keyword.
  • Open the barcode image using the open() function by passing the image path as an argument to it.
  • Pass the above barcode image as an argument to the decode() function to extract the information from the given barcode.
  • Print the above barcode information.
  • Loop in the above barcode information using the for loop.
  • Decode the barcode information by removing the unnecessary information from the above barcode information using the data.decode() function by passing “utf-8” as an argument to it and print it
  • The Exit of the Program.

Below is the implementation:

# Import decode function from pyzbar module using the import keyword
from pyzbar.pyzbar import decode
# Import Image function from PIL module using the import keyword
from PIL import Image 
# Open the barcode image using the open() function by passing the image path as an argument to it.
gvn_barimag = Image.open(r"C:\Users\vicky\Downloads\barcodesample.jpg")    
# Pass the above barcode image as an argument to the decode() function to extract the 
# information from the given barcode.
# (This info is so clumsy that nothing can be decrypted from it)
barcode_informatn = decode(gvn_barimag)
# Print the above barcode information
print(barcode_informatn)
print('\nPrinting the BARCODE INFORMATION :')
# Loop in the above barcode information using the for loop
for k in barcode_informatn:
    # Decode the barcode information by removing the unnecessary information
    # from the above barcode information using the data.decode() function
    # by passing "utf-8" as argument to it
    print(k.data.decode("utf-8"))

Output:

[Decoded(data=b'0712345678911', type='EAN13', rect=Rect(left=88, top=0, width=760, height=497), polygon=[Point(x=88, y=1), Point(x=88, y=497), Point(x=456, y=497), Point(x=848, y=496), Point(x=848, y=0)])]

Printing the BARCODE INFORMATION :
0712345678911