Python二维码生成识别实例详解
作者:小柒 时间:2021-06-10 19:59:22
前言
在 JavaWeb 开发中,一般使用 Zxing 来生成和识别二维码,但是,Zxing 的识别有点差强人意,不少相对模糊的二维码识别率很低。不过就最新版本的测试来说,识别率有了现显著提高。
对比
在没接触 Python 之前,曾使用 Zbar 的客户端进行识别,测了大概几百张相对模糊的图片,Zbar的识别速度要快很多,识别率也比 Zxing 稍微准确那边一丢丢,但是,稍微模糊一点就无法识别。相比之下,微信和支付宝的识别效果就逆天了。
代码案例
# -*- coding:utf-8 -*-
import os
import qrcode
import time
from PIL import Image
from pyzbar import pyzbar
"""
# 升级 pip 并安装第三方库
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__":
# # 简易版
# 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.")
#
# # 参数版
# 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.")
# 识别答题卡二维码 16 识别失败
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("识别失败数量:" + str(count))
print("测试时间:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))
测试了32张精挑细选的模糊二维码:
识别失败数量:1
测试时间:130
使用最新版的 Zxing 识别失败了三张。
源码
https://gitee.com/52itstyle/Python/tree/master/Day13
来源:https://blog.52itstyle.vip/archives/3724/
标签:python,二维码,生成,识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JS二维数组的定义说明
2023-08-23 15:09:45
python 统计代码耗时的几种方法分享
2023-11-03 19:51:06
![](https://img.aspxhome.com/file/2023/4/63274_0s.png)
Python变量及数据类型用法原理汇总
2022-12-04 11:11:41
Oracle SQL性能优化系列学习三
2010-07-23 13:08:00
FrontPage XP设计教程4——Css样式表的应用
2008-10-11 12:25:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017123534164s.jpg)
Python PaddleNLP开源实现快递单信息抽取
2023-01-21 04:35:11
![](https://img.aspxhome.com/file/2023/3/80603_0s.jpg)
asp如何将统计的访问者数目周期性地保存?
2009-11-26 20:54:00
python pip源配置,pip配置文件存放位置的方法
2023-01-25 09:51:46
我所钟爱的HTML5资源
2010-07-23 09:25:00
![](https://img.aspxhome.com/file/UploadPic/20107/23/01-73s.jpg)
CPQuery 解决拼接SQL的新方法
2012-11-30 20:01:46
基于jsp实现新闻管理系统 附完整源码
2023-07-10 15:35:53
![](https://img.aspxhome.com/file/2023/2/61352_0s.png)
DTS构建组件及其如何完成数据转换服务
2009-01-20 15:37:00
Flask项目的部署的实现步骤
2023-08-11 17:59:58
![](https://img.aspxhome.com/file/2023/9/85319_0s.jpg)
python excel多行合并的方法
2021-09-25 22:36:29
![](https://img.aspxhome.com/file/2023/3/81823_0s.png)
python将音频进行变速的操作方法
2023-10-05 19:04:34
![](https://img.aspxhome.com/file/2023/1/89401_0s.png)
html+css+js实现别踩白板小游戏
2023-09-02 10:05:42
![](https://img.aspxhome.com/file/2023/4/56064_0s.png)
JupyterLab远程密码访问实现
2022-06-04 23:52:26
![](https://img.aspxhome.com/file/2023/9/89319_0s.png)
Python切片知识解析
2022-06-07 06:31:58
解析ASP的Application和Session对象
2007-09-14 10:13:00
php处理json格式数据经典案例总结
2023-11-21 13:54:42