python生成二维码
需要用到的包
pip install qrcode
代码:
import qrcode
from PIL import Image
data = "www.baidu.com"
img_name = '二维码.png'
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image()
img = img.convert("RGBA")
'''
这段为添加图片logo代码 start
'''
icon = Image.open("logo.png")
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)
'''
这段为添加图片logo代码 end
'''
img.save(img_name)
img.show()