1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import os import qrcode from PIL import ImageFont, ImageDraw, Image
text = "https://kevinvane.github.io"
img_path = './qrcode_test3.png' img_qr = qrcode.make(text)
font_pingfang = ImageFont.truetype("./font/pingfang.ttf", 22) font_width, font_height = font_pingfang.getsize(text)
qr_width, qr_height = img_qr.size image = Image.new( mode="RGB", size=(qr_width, qr_height + font_height), color="white") image.paste(img_qr) img_qr.close()
draw = ImageDraw.Draw(image)
xy = ((qr_width - font_width)/2, qr_height - 20) draw.text(xy, text, font=font_pingfang, fill=(0, 0, 0)) image.save(img_path)
|