美文网首页
python生成二维码并输出图片流

python生成二维码并输出图片流

作者: rgbRed | 来源:发表于2020-03-04 20:25 被阅读0次

先安装生成qrcode所需要的库:

pip3 install qrcode
pip3 install Pillow
pip3 install img

在安装输出图片流的库

pip3 install six

开始生成qrcode:

import qrcode
from six import BytesIO
def QRcode(url):
    qr = qrcode.QRCode(
        version=2,  # 尺寸大小
        error_correction=qrcode.constants.ERROR_CORRECT_L,  # 容错系数 ERROR_CORRECT_L: 7%的字码可被容错
                                                            # ERROR_CORRECT_M: 15%的字码可被容错
                                                            # ERROR_CORRECT_Q: 25%的字码可被容错
                                                            # ERROR_CORRECT_H: 30%的字码可被容错
        box_size=10,    # 每个格子像素大小
        border=1    # 边框距离
    )
    qr.add_data(url)
    qr.make(fit=False)
    img = qr.make_image()
    buf = BytesIO()
    img.save(buf)
    image_stream = buf.getvalue()
    return image_stream

相关文章

网友评论

      本文标题:python生成二维码并输出图片流

      本文链接:https://www.haomeiwen.com/subject/qiuilhtx.html