一、准备工作
1. pip instaill image & pip instaill qrcode
2.png 格式的图片,命名为logo.png,放在根目录下
二、代码
importqrcodeasqrcode
fromPILimportImage
url ="http://www.jianshu.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")
icon = Image.open("./logos.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
ificon_w > size_w:
icon_w = size_w
ificon_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.save("./test.png")
运行就ok了,试试吧?