Python二維碼生成識(shí)別實(shí)例詳解
前言
在 JavaWeb 開(kāi)發(fā)中,一般使用 Zxing 來(lái)生成和識(shí)別二維碼,但是,Zxing 的識(shí)別有點(diǎn)差強(qiáng)人意,不少相對(duì)模糊的二維碼識(shí)別率很低。不過(guò)就最新版本的測(cè)試來(lái)說(shuō),識(shí)別率有了現(xiàn)顯著提高。
對(duì)比
在沒(méi)接觸 Python 之前,曾使用 Zbar 的客戶(hù)端進(jìn)行識(shí)別,測(cè)了大概幾百?gòu)埾鄬?duì)模糊的圖片,Zbar的識(shí)別速度要快很多,識(shí)別率也比 Zxing 稍微準(zhǔn)確那邊一丟丟,但是,稍微模糊一點(diǎn)就無(wú)法識(shí)別。相比之下,微信和支付寶的識(shí)別效果就逆天了。
代碼案例
# -*- coding:utf-8 -*-
import os
import qrcode
import time
from PIL import Image
from pyzbar import pyzbar
"""
# 升級(jí) pip 并安裝第三方庫(kù)
pip install -U pip
pip install Pillow
pip install pyzbar
pip install qrcode
"""
def make_qr_code_easy(content, save_path=None):
"""
Generate QR Code by default
:param content: The content encoded in QR Codeparams
:param save_path: The path where the generated QR Code image will be saved in.
If the path is not given the image will be opened by default.
"""
img = qrcode.make(data=content)
if save_path:
img.save(save_path)
else:
img.show()
def make_qr_code(content, save_path=None):
"""
Generate QR Code by given params
:param content: The content encoded in QR Code
:param save_path: The path where the generated QR Code image will be saved in.
If the path is not given the image will be opened by default.
"""
qr_code_maker = qrcode.QRCode(version=2,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=8,
border=1,
)
qr_code_maker.add_data(data=content)
qr_code_maker.make(fit=True)
img = qr_code_maker.make_image(fill_color="black", back_color="white")
if save_path:
img.save(save_path)
else:
img.show()
def make_qr_code_with_icon(content, icon_path, save_path=None):
"""
Generate QR Code with an icon in the center
:param content: The content encoded in QR Code
:param icon_path: The path of icon image
:param save_path: The path where the generated QR Code image will be saved in.
If the path is not given the image will be opened by default.
:exception FileExistsError: If the given icon_path is not exist.
This error will be raised.
:return:
"""
if not os.path.exists(icon_path):
raise FileExistsError(icon_path)
# First, generate an usual QR Code image
qr_code_maker = qrcode.QRCode(version=4,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=8,
border=1,
)
qr_code_maker.add_data(data=content)
qr_code_maker.make(fit=True)
qr_code_img = qr_code_maker.make_image(fill_color="black", back_color="white").convert('RGBA')
# Second, load icon image and resize it
icon_img = Image.open(icon_path)
code_width, code_height = qr_code_img.size
icon_img = icon_img.resize((code_width // 4, code_height // 4), Image.ANTIALIAS)
# Last, add the icon to original QR Code
qr_code_img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8))
if save_path:
qr_code_img.save(save_path)
else:
qr_code_img.show()
def decode_qr_code(code_img_path):
"""
Decode the given QR Code image, and return the content
:param code_img_path: The path of QR Code image.
:exception FileExistsError: If the given code_img_path is not exist.
This error will be raised.
:return: The list of decoded objects
"""
if not os.path.exists(code_img_path):
raise FileExistsError(code_img_path)
# Here, set only recognize QR Code and ignore other type of code
return pyzbar.decode(Image.open(code_img_path), symbols=[pyzbar.ZBarSymbol.QRCODE], scan_locations=True)
if __name__ == "__main__":
# # 簡(jiǎn)易版
# make_qr_code_easy("make_qr_code_easy", "make_qr_code_easy.png")
# results = decode_qr_code("make_qr_code_easy.png")
# if len(results):
# print(results[0].data.decode("utf-8"))
# else:
# print("Can not recognize.")
#
# # 參數(shù)版
# make_qr_code("make_qr_code", "make_qr_code.png")
# results = decode_qr_code("make_qr_code.png")
# if len(results):
# print(results[0].data.decode("utf-8"))
# else:
# print("Can not recognize.")
#
# 帶中間 logo 的
# make_qr_code_with_icon("https://blog.52itstyle.vip", "icon.jpg", "make_qr_code_with_icon.png")
# results = decode_qr_code("make_qr_code_with_icon.png")
# if len(results):
# print(results[0].data.decode("utf-8"))
# else:
# print("Can not recognize.")
# 識(shí)別答題卡二維碼 16 識(shí)別失敗
t1 = time.time()
count = 0
for i in range(1, 33):
results = decode_qr_code(os.getcwd()+"\\img\\"+str(i)+".png")
if len(results):
print(results[0].data.decode("utf-8"))
else:
print("Can not recognize.")
count += 1
t2 = time.time()
print("識(shí)別失敗數(shù)量:" + str(count))
print("測(cè)試時(shí)間:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))
測(cè)試了32張精挑細(xì)選的模糊二維碼:
識(shí)別失敗數(shù)量:1 測(cè)試時(shí)間:130
使用最新版的 Zxing 識(shí)別失敗了三張。
源碼
https://gitee.com/52itstyle/Python/tree/master/Day13
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法,涉及Python調(diào)用系統(tǒng)win32com組件實(shí)現(xiàn)文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-07-07
Python使用GitPython操作Git版本庫(kù)的方法
這篇文章主要介紹了Python使用GitPython操作Git版本庫(kù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python Selenium XPath根據(jù)文本內(nèi)容查找元素的方法
這篇文章主要介紹了Python Selenium XPath根據(jù)文本內(nèi)容查找元素的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
在SAE上部署Python的Django框架的一些問(wèn)題匯總
這篇文章主要介紹了在SAE上部署Python的Django框架的一些問(wèn)題匯總,SAE是新浪的一個(gè)在線APP部署平臺(tái),并且對(duì)Python應(yīng)用提供相關(guān)支持,需要的朋友可以參考下2015-05-05
pycharm運(yùn)行程序時(shí)出現(xiàn)Run‘python tests for XXX.py‘問(wèn)題及
這篇文章主要介紹了pycharm運(yùn)行程序時(shí)出現(xiàn)Run ‘python tests for XXX.py‘問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python通過(guò)paramiko庫(kù)實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法
這篇文章主要介紹了Python通過(guò)paramiko庫(kù)實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
python查看矩陣的行列號(hào)以及維數(shù)方式
這篇文章主要介紹了python查看矩陣的行列號(hào)以及維數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python+tkinter實(shí)現(xiàn)一個(gè)繪圖風(fēng)格控件
這篇文章主要為大家詳細(xì)介紹了Python如何利用tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的繪圖風(fēng)格控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09

