python使用pyzbar批量识别图片中的二维码

欢迎关注原创视频教程

Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475

背景

今天QQ群里面有人问到谁有二维码提取的工具,样子如下python使用pyzbar批量识别图片中的二维码

我一看这个工具,感觉功能实现上比较简单,但我后来想了想这个工具功能虽然简单,但使用的人群还是比较多的,于是今天抽出几分钟的时间把这个工具的核心代码撸了撸,虽然没有做GUI,但运行代码基本上功能都实现了

准备要识别的二维码图片

要识别的二维码图片()如下:python使用pyzbar批量识别图片中的二维码

准备python环境

我的python环境是python3.8 创建目录qrcode目录,进入改目录执行如下代码

cd qrcode

python -m venv venv

\venv\Scripts\Activate.ps1 # windows powershell 执行进入python虚拟环境

# 安装 pyzbar 模块
pip install pyzbar
#安装 PIL库
pip install pillow

实现简单的单张二维码图片识别代码

from PIL import Image
import pyzbar.pyzbar as pyzbar

img_path = 'qrcode.png'
img = Image.open(img_path)

#使用pyzbar解析二维码图片内容
barcodes = pyzbar.decode(img)

#打印解析结果,从结果上可以看出,data是识别到的二维码内容,rect是二维码所在的位置
print(barcodes)
# [Decoded(data=b'http://www.h3blog.com', type='QRCODE', rect=Rect(left=7, top=7, width=244, height=244), polygon=[Point(x=7, y=7), Point(x=7, y=251), Point(x=251, y=251), Point(x=251, y=7)])]

for barcode in barcodes:
    barcode_content = barcode.data.decode('utf-8') # 二维码内容
    print(barcode_content)
    barcode_rect = barcode.rect # 二维码在图片中的位置
    qr_size = list(barcode_rect) #二维码大小
    print(qr_size)

批量识别图片二维码

我们把上面的代码重构下,做一个批量识别二维码的小工具,程序读取指定目录下面的图片,将识别结果保存到txt文件中

代码如下保存成 qrcode.py:

from PIL import Image
import pyzbar.pyzbar as pyzbar
import os

def qrcode_parse_content(img_path):
    '''
    单张图片的二维码解析
    '''
    img = Image.open(img_path)

    #使用pyzbar解析二维码图片内容
    barcodes = pyzbar.decode(img)

    #打印解析结果,从结果上可以看出,data是识别到的二维码内容,rect是二维码所在的位置
    # print(barcodes)
    # [Decoded(data=b'http://www.h3blog.com', type='QRCODE', rect=Rect(left=7, top=7, width=244, height=244), polygon=[Point(x=7, y=7), Point(x=7, y=251), Point(x=251, y=251), Point(x=251, y=7)])]

    result = []
    for barcode in barcodes:
        barcode_content = barcode.data.decode('utf-8')
        result.append(barcode_content)

    return result

def load_imgs(folder):
    '''
    加载文件夹下的图片
    '''
    imgs = []
    for img_path in os.listdir(folder):
        ext = os.path.splitext(img_path)
        if len(ext) > 1 and is_img(ext[1]):
            imgs.append(img_path)

    return imgs

def is_img(ext):
    '''
    判断文件后缀是否是图片
    '''
    ext = ext.lower()
    if ext == '.jpg':
        return True
    elif ext == '.png':
        return True
    elif ext == '.jpeg':
        return True
    elif ext == '.bmp':
        return True
    else:
        return False

if __name__ == "__main__":

    imgs = load_imgs('./') # 打开图片文件夹,我这里是当前程序运行目录
    contents = []
    for img in imgs:
        contents.extend(qrcode_parse_content(img))

    file = './result.txt' # 写入文件路径,我这里程序当前运行目录下的result.txt
    with open(file,'w') as f:
        for c in contents:
            f.write(c + '\n')

运行

python qrcode.py

总结

这里面注意是运用pyzbar识别二维码,整个过程比较简单,上述代码供大家参考,当然加上GUI界面操作就有一点点的小逼格了,2333333

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚坏叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值