Python二维码生成识别实例详解
作者:小柒 发布时间:2021-06-10 19:59:22
标签:python,二维码,生成,识别
前言
在 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/
0
投稿
猜你喜欢
- 前言本文主要记录python下音频常用的操作,以.wav格式文件为例。其实网上有很多现成的音频工具包,如果仅仅调用,工具包是更方便的。更多p
- 本文所用环境:Python 3.6.5 |Anaconda custom (64-bit)|引言由于某些原因,需要用python读取二进制文
- 通过学习装饰器可以让我们更好更灵活的使用函数,通过学会使用装饰器还可以让我们的代码更加优雅。在我们的实际工作中,很多场景都会用到装饰器,比如
- 一、图像的加法图像相加可以直接利用numpy模块进行相加,也可以采用opencv里面函数进行相加,注意事项:相加的图像类型、大小必须相同具体
- 一行命令搭建一个基于python的http文件传输服务由于今天朋友想要一个文件,而我恰好有,因为这个文件比较大,网速不是很给力,所以想到了p
- 背景一直对语音合成系统比较感兴趣,总想能给自己合成一点内容,比如说合成小说,把我下载的电子书播报给我听等等。语音合成系统其实就是一个基于语音
- 发现一个有意思的现象,labelimg打开图片和xml标签时候,看不到标注好的框框,仔细查看了xml文件,没发现什么异常,后面试一下,才发现
- 在获得网页响应对象res后,使用res.text属性可以获得网页源代码,但可能出现乱码!因为requests库会使用自动猜测的解码方式将抓取
- 1. resize函数说明 OpenCV提供了resize函数来改变图像的大小,函数原型如下:void resiz
- Python遍历numpy数组下面是示例代码:import numpy as npnum = np.zeros([2, 3])[rows,
- 为了庆祝jQuery的四周岁生日, jQuery的团队荣幸的发布了jQuery Javascript库的最新主要版本! 这个版本包含了大量的
- join 方法用于连接字符串数组 s = ['a', 'b', 'c', 'd
- python版本:3.8class object: """ The most base type "
- 关于Django生成迁移文件,我是在虚拟机上完成的1.创建虚拟环境:在终端上输入创建python3的虚拟环境mkvirtualenv -p
- 表单的验证一直是网页设计者头痛的问题,表单验证类 Validator就是为解决这个问题而写的,旨在使设计者从纷繁复杂的表单验证中解放出来,把
- 前言现在Python3 被越来越多的开发者所接受,同时让人尴尬的是很多遗留的老系统依旧运行在 Python2 的环境中,因此有时你不得不同时
- 凡搞WEB开发的人都离不开HTTP(超文本传输协议),而要了解HTTP,除了HTML本身以外,还有一部分不可忽视的就是HTTP消息头。做过S
- 过滤html代码的函数,当然也可以使用正则表达式。<%Function FilterHTML(strToFilter)&nb
- 具体代码如下所示:import numpy as npfrom matplotlib import pyplot as pltfrom sc
- 现在,比较牛的设计师和开发者都认识到了可用性在他们工作中的重要性。可用性好的网站会极大地提高用户体验,并且好的用户体验会让用户更加快乐。用聪