Python 生成logo二维码 django 返回

url(r'^label_qrcode/(?P<label_num>\S+)/$',views.label_qrcode, name='label-qrcode'),


def label_qrcode(request,label_num):
    """  生成二维码 """
    url = WX_URL.format(label_num)
    # 生成二维码
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=3
    )
    qr.add_data(url)
    qr.make(fit=True)
    img = qr.make_image()
    img = qr.make_image()
    # 设置二维码为彩色
    img = img.convert("RGBA")
    icon_path = BASE_DIR+"/saleor/static/assets/favicons/android-chrome-96x96.png"
    icon = Image.open(icon_path)
    w, h = img.size
    factor = 4
    size_w = int(w / factor)
    size_h = int(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((w - icon_w) / 2)
    h = int((h - icon_h) / 2)
    icon = icon.convert("RGBA")
    newimg = Image.new("RGBA", (icon_w + 8, icon_h + 8), (255, 255, 255))
    img.paste(newimg, (w - 4, h - 4), newimg)
    img.paste(icon, (w, h), icon)
    img.save('media/qrcode.png')
    image_path = BASE_DIR + "/media/qrcode.png"
    image_data = open(image_path, "rb").read()
    return HttpResponse(image_data, content_type="image/png")

猜你喜欢

转载自blog.csdn.net/weixin_37989267/article/details/81914335