Python生成带有网页链接的二维码

使用python生成一个二维码,微信扫描后可跳转到指定网页

参考链接:https://blog.csdn.net/cungudafa/article/details/85871871

1:安装相关库

pip3 install qrcode
pip3 install Image
pip install myqr

2: 代码
import qrcode
from MyQR import myqr
from PIL import Image

def QR_With_FullBackground_Img(link="http://192.168.1.10:8080", background_picture="BackgroundIMG.png", outputput_file="output_code_with_background_png.png"):
    #图片布满整个二维码
    myqr.run(
        words=link,#Link address
        version=1,  # 设置容错率为最高默认边长是取决于你输入的信息的长度和使用的纠错等级;而默认纠错等级是最高级的H
        level='H',  # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
        picture=background_picture,  # 用来将QR码图像与一张同目录下的图片相结合,产生一张黑白图片,格式可以是.jpg, .png, .bmp, .gif
        colorized=True,  # 可以使产生的图片由黑白(False)变为彩色(True)的
        contrast=1.0,  # 用以调节图片的对比度,1.0 表示原始图片,更小的值表示更低对比度,更大反之。默认为1.0。
        brightness=1.0,  # 用来调节图片的亮度
        save_name=outputput_file# 输出文件名字
    )


def QR_With_Central_Img(link="http://192.168.1.10:8080", central_picture="BackgroundIMG.png", outputput_file="output_code_with_central_png.png"):
    #图片在二维码中心位置
    qr = qrcode.QRCode(
    version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=8, border=4)
    qr.add_data(link)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    icon = Image.open(central_picture)  # 这里是二维码中心的图片

    img_w, img_h = img.size
    factor = 4
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)

    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)

    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    icon = icon.convert("RGBA")
    img.paste(icon, (w, h), icon)
    img.save(outputput_file)
    #img.show()  # 显示图片,可以通过save保存

    
def QR_Single_Code(link="http://192.168.1.10:8080", outputput_file="output_code_simple.png"):
    #简单的二维码
    # 生成二维码实例,设置大小和边框
    qr = qrcode.QRCode(box_size=10, border=2)
    # 添加二维码的显示信息
    content = link
    qr.add_data(content)
    qr.make(fit=True)
    img = qr.make_image()
    # 保存二维码
    img.save(outputput_file)
    

if __name__ == '__main__':
    #link: url
    #background_picture: background picture filename
    #outputput_file: output filename
    QR_With_FullBackground_Img(link="https://blog.csdn.net", background_picture="BackgroundIMG.png", outputput_file="output_code_with_background_png.png")
    
    #link: url
    #central_picture: central picture filename
    #outputput_file: output filename
    QR_With_Central_Img(link="https://stackoverflow.com", central_picture="BackgroundIMG.png", outputput_file="output_code_with_central_png.png")
    
    #link: url
    #outputput_file: output filename
    QR_Single_Code(link="https://github.com/", outputput_file="output_code_simple.png")
3:同目录下放一个png图片就可以生成带有图片的二维码了

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值