python生成二维码

# -*- coding: utf-8 -*-
# @Author  : FELIX
# @Date    : 2018/5/18 16:19

import qrcode
from PIL import Image

url='http://www.baidu.com'
qr=qrcode.QRCode(
    version=2,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=8,
    border=2
    )
qr.add_data(url)  # 添加信息
qr.make(fit=True)
img=qr.make_image()
img=img.convert('RGBA')


# im = Image.open(r"cc.jpg")
# im.save(r"c2.png")

# 添加logo时logo的格式为png,不然会报错
icon=Image.open('ab.png')
img_w,img_h=img.size
factor=5  # 比例,过小可能无法识别
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)
img.paste(icon, (w, h), icon)
# img=qrcode.make('http://www.baidu.com')
img.save('test.png')

相关推荐