import qrcode
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# 二维码图片地址
QR_IMG = '/Users/admin/Desktop/project/imgs/'
# 字体地址
TTF_BIR = '/Users/admin/Desktop/project/msyh.ttf'
# 背景图片地址(我用的是一个空白图片)
BG_BIR = "/Users/admin/Desktop/project/bg.png"
# 生成二维码
def make_qr(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2,
)
# 添加数据
qr.add_data(data)
# 填充数据
qr.make(fit=True)
# 生成图片
img = qr.make_image(fill_color="black", back_color="white")
img = img.convert("CMYK") # RGBA
img = img.convert('RGB')
img.save(QR_IMG + data + '.png')
# 填充文字
def nature_info(num, nature):
oriImg = Image.open(QR_IMG + num + ".png")
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20) # 设置字体
# 字体的位置 、颜色
draw.text((0, 40), '性质: ' + nature, (50, 51, 51), font=font) # 把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
# 填充文字
def category_info(num, category):
oriImg = Image.open(QR_IMG + num + ".png")
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20) # 设置字体
# 字体的位置 、颜色
draw.text((0, 80), '品类: ' + category, (50, 51, 51), font=font) # 把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
# 填充字体
def product_name_info(num, product_name):
oriImg = Image.open(QR_IMG + num + ".png")
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20) # 设置字体
# 字体的位置 、颜色
draw.text((0, 120), '品名: ' + product_name, (50, 51, 51), font=font) # 把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
# 填充字体
def color_info(num, colors):
oriImg = Image.open(QR_IMG + num + ".png")
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20) # 设置字体
# 字体的位置 、颜色
draw.text((0, 160), '颜色: ' + colors, (50, 51, 51), font=font) # 把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
# 填充字体
def specifications_info(num, specifications):
oriImg = Image.open(QR_IMG + num + ".png")
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20) # 设置字体
# 字体的位置 、颜色
draw.text((0, 200), '规格: ' + specifications, (50, 51, 51), font=font) # 把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
# 填充字体
def level_info(num, level):
oriImg = Image.open(QR_IMG + num + ".png")
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20) # 设置字体
# 字体的位置 、颜色
draw.text((0, 240), '等级: ' + level, (50, 51, 51), font=font) # 把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
# 在背景图片上布局二维码和字体
def info(num, nature_name, category_name, product_name, colors_name, specifications_name, level_name):
make_qr(num)
oriImg = Image.open(BG_BIR)
oriImg2 = Image.open(QR_IMG+ num + '.png')
oriImg2 = oriImg2.resize((250, 250))#设置二维码大小
oriImg.paste(oriImg2, (150, 50))#将二维码放在底图上
draw = ImageDraw.Draw(oriImg)
font = ImageFont.truetype(TTF_BIR, 20)#设置字体
# 编号
draw.text((0, 0), '编号: '+num, (50, 51, 51), font=font)#把字添加到图片上
oriImg = oriImg.convert('RGB')
oriImg.save(QR_IMG + num + '.png')
nature_info(num, nature_name)
category_info(num, category_name)
product_name_info(num, product_name)
color_info(num, colors_name)
specifications_info(num, specifications_name)
level_info(num, level_name)
if __name__ == '__main__':
info("800000848668", 'df', 'fgr', '皇冠', 'er', 'etgh', 'jhgf')
print("nice,nice,nice,nice")