简单地使用cv2
和pylibdmtx
如下
import cv2
from pylibdmtx.pylibdmtx import decode
image = cv2.imread("1591106831_festo.jpg")
h, w = image.shape[:2]
decdd = decode((image[:, :, :1].tobytes(), w, h))
print(decdd)
# [Decoded(data=b'(91)4608', rect=Rect(left=650, top=522, width=-82, height=86))]
这可能比您目前手头的更快,因为我们1)提供decode
了不可猜测的参数,并且2)提供了,这image[:, :, :1]
意味着一个只处理第一个BGR层,蓝色层,显然足以用于条形码.
为了比较,
decode(Image.open("1591106831_festo.jpg"))
返回
[Decoded(data=b'(91)4608', rect=Rect(left=650, top=522, width=-82, height=86))]
即完全相同的推论。
你可以做的是限制你想获得的条形码数量,使用
max_count
参数,
>>> decode(Image.open("1591106831_festo.jpg"), max_count=1)
[Decoded(data=b'(91)4608', rect=Rect(left=522, top=629, width=86, height=82))]