qrcode库是一个生成二维码的Python库,可以在Python中轻松地生成QR码。
安装qrcode库需要使用pip命令: pip install qrcode
生成QR码的基本格式如下:
import qrcode
img = qrcode.make('Hello World!')
img.show()
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Hello World!')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.show()
import qrcode
from PIL import Image
# 生成二维码
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Hello World!')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
# 添加Logo
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)
img.paste(icon, (w, h), mask=None)
# 显示二维码
img.show()
更多关于qrcode库的用法可以参考以下链接: