pythonnumpyopencvzxingpdf417

How can i pass image itself (np.array) not path of it to zxing library for decode pdf417


Code:

import zxing
from PIL import Image

reader = zxing.BarCodeReader()
path = 'C:/Users/UI UX/Desktop/Uasa.png'
im = Image.open(path)

barcode = reader.decode(path)
print(barcode)

when i use code above work fine and return result: BarCode(raw='P<E....

i need to use this code:

import zxing
import cv2

reader = zxing.BarCodeReader()
path = 'C:/Users/UI UX/Desktop/Uasa.png'

img = cv2.imread (path)
cv2.imshow('img', img)
cv2.waitKey(0)

barcode = reader.decode(img)
print(barcode)

but this code return an error: TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray

In another program i have image at base64 could help me somewhere here?

any body could help me with this?


Solution

  • ZXing does not support passing an image directly as it is using an external application to process the barcode image. If you're not locked into using the ZXing library for decoding PDF417 barcodes you can take a look at the PyPI package pdf417decoder.

    If you're starting with a Numpy array like in your example then you have to convert it to a PIL image first.

    import cv2
    import pdf417decoder
    from PIL import Image
    
    npimg = cv2.imread (path)
    cv2.imshow('img', npimg)
    cv2.waitKey(0)
    
    img = Image.fromarray(npimg)
    decoder = PDF417Decoder(img)
    
    if (decoder.decode() > 0):
        print(decoder.barcode_data_index_to_string(0))
    else:
        print("Failed to decode barcode.")