qr_code.py 3.8 KB
import os
import sys
import qrcode
# from PIL import Image
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import numpy
from app import app
from flask import jsonify,render_template,request,send_from_directory
import matplotlib.font_manager as fm
import logging

code_path = '/prog/smartshelf/app/static/uploads'

def make_qrcode(file_code):
    # for line in all_data:
    #     A_NUMBER = line[0]
    #     c_start = int(line[1])
    #     c_stop = int(line[2])
    #     dirs_str = deal_line_code(project_number,A_NUMBER,c_start,c_stop)
    #     resize(project_number,dirs_str)
    # current_path_file=os.path.join(code_path,t_file)
    qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=0)        
    qr.add_data(file_code)
    qr.make(fit=True)
    img = qr.make_image()
    file = "{}/{}.png".format(code_path,file_code)
    img.save(file)
    # img1.save("./{}_qr_jpg/{}/{}.jpg".format(project_number,dirs_str,filename[:-4]))
    return True

def resize(file_code):
    # path = './{}_qr_png/{}'.format(project_number,dirs_str)
    # path_list = os.listdir(path)
    img1 = Image.open("{}/{}.png".format(code_path,file_code))
    # img1 = Image.open("E:/qr_png/code/0/{}.png".format(filename))
    img1 = img1.resize((300, 300),Image.ANTIALIAS)
    # print (filename[:-4])
    img1.save("{}/{}.jpg".format(code_path,file_code))

def deal_number(file_code):
    print (file_code)
    color = "White"
    img_str = Image.new("RGB",(480,90),color)
    img_str.save("{}/temp_number.jpg".format(code_path))
    im = Image.open("{}/temp_number.jpg".format(code_path))
    # font = ImageFont.truetype('simsun.ttc',55)
    # font = ImageFont.truetype(fm.findfont(fm.FontProperties(family='DejaVu Sans')),63)
    if os.path.isfile("/usr/share/fonts/truetype/dejavu/simsun.ttf"):
        font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/simsun.ttf",75)
        draw = ImageDraw.Draw(im)
        draw.text((50,20), file_code, font=font, fill='#000')
    else:
        font = ImageFont.truetype(fm.findfont(fm.FontProperties(family='DejaVu Sans')),75)
        draw = ImageDraw.Draw(im)
        draw.text((30,20), file_code, font=font, fill='#000')
    # draw.text((60,13), filename, font=font, fill='#000')  #602项目改动
    im.save("{}/temp_number.jpg".format(code_path))

def paste(file_code):
    color = 'white'
    target = Image.new("RGB",(300,390),color)
    # qr = Image.open("E:/qr_png/jpg_code/B/{}".format(filename))
    # qr = Image.open("E:/code_tool/{}/{}/{}.jpg".format(source_path,path_str,filename))
    qr = Image.open('{}/{}.jpg'.format(code_path,file_code))
    im = Image.open('{}/temp_number.jpg'.format(code_path))
    a = 0  # 图片距离左边的大小
    b = 0  # 图片距离上边的大小
    c = 300  # 图片距离左边的大小 + 图片自身宽度
    d = 300  # 图片距离上边的大小 + 图片自身高度
    target.paste(qr, (a, b, c, d))
    e = 0
    f = 300
    # target.paste(im, (e, f))
    target.save("{}/code_img_str.jpg".format(code_path))
    targeta = Image.new("RGB",(480,480),color)
    targetb = Image.open("{}/code_img_str.jpg".format(code_path))
    targeta.paste(targetb, (90, 60))
    targeta.paste(im, (0, 360))
    targeta.save("{}/{}.jpg".format(code_path,file_code))
    logging.warning("库位二维码生成成功:{}".format(file_code))

@app.route('/downqrcode/', methods=['GET','POST'], strict_slashes=False)
def downqrcode():
    # file_code = '001-01-001'
    file_code = str(request.args.get("code"))
    make_qrcode(file_code)
    resize(file_code)
    deal_number(file_code)
    paste(file_code)
    c_path = '/prog/smartshelf/app/static/uploads/'
    filename = '{}.jpg'.format(file_code)
    if request.method == "GET":
        if os.path.isfile(os.path.join(c_path, filename)):
            return send_from_directory(c_path, filename, as_attachment=True)