相同的代码、相同的python版本、相同的导入包版本,运行的是不一样的结果
from pyzbar import pyzbar
from PIL import Image
decocdeQR = pyzbar.decode(Image.open("C:\Users\Administrator\Desktop\test\test.jpg"))
print(decocdeQR)
我本地运行识别为空,网友能正常识别到内容url
请教一个网友识别二维码,之前我尝试过这种方法但是识别不出来,但是网友的电脑可以识别出来拿到结果url,我识别为空,尝试版本归结一致去解决问题,但貌似没多大效果,
pyzbar 0.1.9
pillow 9.2.0
正常情况下应该相同的代码运行结果一样的,奇怪的是我拿不到结果<下面是待识别的二维码>
文件路径要加上一个r ,比如上面的写成r"C:\Users\Administrator\Desktop\test\test.jpg"
不行可以换另一种:
import zxing
reader = zxing.BarCodeReader()
barcode = reader.decode(r"C:\Users\Administrator\Desktop\test\test.jpg")
print(barcode.parsed)
--result
http://esf.whfgxx.org.cn/New/pub/RentView/20220514ZLXfRVE?refer=OJClCFBCL01YJJ3xrCMq283TX54J8UNJTOmcrONE&corp=6&org=100003138
解决方案是:你可以写两个函数,当用第一个得到为空的时候,用第二个函数
下面的识别度更好些:
import numpy as np
import cv2
from pyzbar import pyzbar
def get_qrcode(image_input, binary_max=230, binary_step=2):
if len(image_input.shape) >= 3:
image_input = cv2.cvtColor(image_input, cv2.COLOR_RGB2GRAY)
binary, _ = cv2.threshold(image_input, 0, 255, cv2.THRESH_OTSU)
res = []
while (binary < binary_max) and (len(res) == 0):
binary, mat = cv2.threshold(image, binary, 255, cv2.THRESH_BINARY)
res = pyzbar.decode(mat)
return res
image_file = r"C:\Users\Administrator\Desktop\test\test.jpg"
image = cv2.imdecode(np.fromfile(image_file,
dtype=np.uint8),
cv2.IMREAD_COLOR)
result = get_qrcode(image)
print(str(result[0][0]).replace('b', ''))
ignore