简体   繁体   English

使用pyzbar无法读取同一图像文件中的第二个条形码

[英]Can't read second barcode in the same image file using pyzbar

I'm coding in Python 2.7 and I need to implement a process where I will read a PDF then obtain the image of the first page of the document, then from that image that contains two barcodes obtain the values of both. 我使用Python 2.7进行编码,我需要实现一个过程,在该过程中,我将读取PDF,然后获取文档第一页的图像,然后从包含两个条形码的图像中获取两个值。 As of now these are the two functions I've been working on so far (I need to do a lot of polishing before I move this to an environment): 到目前为止,这是我到目前为止一直在努力的两个功能(在将其移至环境之前,我需要做很多工作):

Python process to obtain the image from the PDF from a Tutorial: Python过程,以从Tutorial中的PDF获取图像:

from wand.image import Image as wi
pdf = wi(filename="test.pdf", resolution=300)
pdfImageTest = pdf.convert("png")
i=1
for img in pdfImage.sequence:
  page = wi
  (image = img)
  page.save(filename="test"+str(i)+".png")
  i+=1

Python process to read the barcodes from an image: Python程序从图像读取条形码:

from pyzbar.pyzbar import decode
from PIL import Image
import cv2
import numpy

decodedObjects = decode(Image.open('test2.png'))
obj = decodedObjects
print(obj)
decodedObjects = decode(cv2.imread('test2.png'))
print(obj)

According to the documentation for decode function in pyzbar, the function will scan all the barcodes contained in the image but as of now for both cases I've used, I'm only obtaining the first barcode in the image. 根据pyzbar中解码功能的文档,该功能将扫描图像中包含的所有条形码,但是到目前为止,对于我使用过的两种情况,我只获得图像中的第一个条形码。 Is there a way to force the function to keep scanning the image or pointing it into a specific location of the image after finishing the process for the first image? 在完成对第一张图像的处理之后,是否可以强制该功能继续扫描图像或将其指向图像的特定位置?

You should use obj.data and iterate over all objects. 您应该使用obj.data并遍历所有对象。 Here's an example: 这是一个例子:

from pyzbar.pyzbar import decode
from PIL import Image
import cv2
import numpy

decodedObjects = decode(Image.open('test2.png'))
obj = decodedObjects
for bar in obj:
  print(bar.data)

By the way, the print statement is replaced with print() function in Python 3. So if you strictly want to use Python 2.7, you should use eg print bar.data . 顺便说一句,在Python 3中将print语句替换为print()函数。因此,如果您严格希望使用Python 2.7,则应使用例如print bar.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM