How to make a QR code with small icons in python

import qrcode
# 处理图片
from PIL import Image

# 生成二维码  版本  边框  box_size  容错率
qr = qrcode.QRCode(
    version=1,    #版本
    border=4,     #边框
    box_size=10,  #box_size: 尺寸
    error_correction=qrcode.constants.ERROR_CORRECT_H     #容错率:允许错误出现的范围和概率。
)
# 添加信息,想要需要的信息
qr.add_data("http://wxa.cli.im/qr/idcode/C_/S_2/ilx5XcD/6a")
# 二维码生成样式
img = qr.make_image(fill_color="#000",back_color="#FFF")
# 添加图片  合二为一
icon = Image.open("标志.jpg")
# 设置比例  两张图片的icon 和 img  1:6
factor = 6
# 获取图片的宽和高
img_w, img_h = img.size
icon_w, icon_h = icon.size
print(img_w,img_h)
print(icon_w,icon_h)
# 图片的宽和高
size_w = int(img_w/6)
size_h = int(img_h/6)
# 完成图片的重置
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))
img.show()

After running, the effect picture is:
Insert picture description here

If you want to know my information, you can scan it and my business card will be displayed.
Remarks : 1. In the code as shown below:

# 获取图片的宽和高
img_w, img_h = img.size
icon_w, icon_h = icon.size
print(img_w, img_h)
print(icon_w, icon_h)

Click Run, in the running interface, the width and height of the QR code and the picture will appear, namely:
Insert picture description here

2. If you want to save this QR code in the file, you can add it at the end of the code

# 保存
img.save("想要给图片取的名字.jpg")

Guess you like

Origin blog.csdn.net/weixin_51766972/article/details/109273934