python实现扫描二维码图片,返回相关信息

目录

一、模块准备

二、代码解析

三、代码展示


一、模块准备

这里需要用到PIL和pyzbar模块

二、代码解析

img = Image.open(image)

Image.open(image)用来读取图片

barcodes = pyzbar.decode(img)

pyzbar.decode(img)用于解析图片信息

因为一张图片可能是一张二维码,也可能里面有许多二维码

因此需要遍历解析出的图片信息并进行utf-8格式的转换

    for barcode in barcodes:
        barcodeData = barcode.data.decode("utf-8")

最后将它们封装成函数即可。

三、代码展示

QRcode_message.py

# -*- coding: utf-8-*-
from PIL import Image
import pyzbar.pyzbar as pyzbar

def QRcode_message(image):
    img = Image.open(image)
    # 因为一张图片可能是一张二维码,也可能里面有许多二维码
    barcodes = pyzbar.decode(img)
    for barcode in barcodes:
        barcodeData = barcode.data.decode("utf-8")
        print(barcodeData)

if __name__ == '__main__':
    QRcode_message('test.jpg')

注意:准备好的图片要和QRcode_message.py在同一目录下

猜你喜欢

转载自blog.csdn.net/knighthood2001/article/details/124021225