import qrcode |
from PIL import Image |
# 生成二维码 |
qr = qrcode.QRCode(version = 1 , box_size = 10 , border = 5 ) |
qr.add_data( "https://www.example.com" ) |
qr.make(fit = True ) |
# 转换为图像 |
img = qr.make_image(fill_color = "black" , back_color = "white" ) |
# 打开logo图像 |
logo = Image. open ( "logo.png" ) |
# 计算logo大小 |
logo_w, logo_h = logo.size |
qr_size = img.size[ 0 ] |
logo_size = int (qr_size * 4 / 20 ) |
if logo_w > logo_size: |
logo_w = logo_size |
if logo_h > logo_size: |
logo_h = logo_size |
logo = logo.resize((logo_w, logo_h), Image.ANTIALIAS) |
# 添加logo |
pos = ((qr_size - logo_w) / / 2 , (qr_size - logo_h) / / 2 ) |
img.paste(logo, pos) |
# 保存图像 |
img.save( "qrcode.png" ) |