图片合成

def create_open_shop_poster(unionid, qr_code, par_data):
    font_path = PosterFontFath.POSTER_FONT_PATH
    image_background = Image.new('RGB', (799, 1080), (255, 255, 255))  # 白色背景
    user = UserWeChat.objects.filter(unionid_nj=unionid).first()
    nick_name = user.nickname
    head_img = user.head_img
    head_img = requests.get(head_img).content
    head_img = Image.open(BytesIO(head_img))  # 店铺logo
    req = urllib2.Request(url=qr_code, data=json.dumps(par_data).encode(encoding='utf-8'))
    res = urllib2.urlopen(req)
    base64str = base64.b64encode(res.read())  # 转成base64格式
    img_b64decode = base64.b64decode(base64str)  # base64解码
    qr_code = BytesIO(img_b64decode)
    qr_code = Image.open(qr_code)  # 小程序二维码
    draw = ImageDraw.Draw(image_background)
    qr_code_font = ImageFont.truetype(font_path, 23)
    nick_name_font = ImageFont.truetype(font_path, 27)
    qr_code = qr_code.resize((180, 180), Image.ANTIALIAS)  # 缩放
    image_background.paste(qr_code, (600, 900))
    root_path = settings.BASE_DIR
    base_img = Image.open(os.path.join(root_path, 'static', 'images', 'poster', '开店海报图1029.png'))
    base_img = base_img.resize((799, 909), Image.ANTIALIAS)  # 缩放
    hand_img = Image.open(os.path.join(root_path, 'static', 'images', 'poster', 'hand.png'))
    image_background.paste(base_img, (0, 0))
    head_img = head_img.resize((80,80), Image.ANTIALIAS)
    head_img = crop_center(head_img,40)  # 头像剪裁成圆形
    image_background.paste(head_img, (30, 910))
    image_background.paste(hand_img, (430, 1000))
    draw.text((130, 910), nick_name.decode('utf-8'), font=nick_name_font, fill="#363636")
    draw.text((130, 950), unicode("正在邀请您", 'UTF-8'), font=nick_name_font, fill="#696969")
    draw.text((270, 950), unicode("免费开店", 'UTF-8'), font=nick_name_font, fill="#FF4500")
    draw.text((130, 1010), unicode("长按识别二维码", 'UTF-8'), font=qr_code_font, fill="#696969")
    draw.text((290, 1010), unicode("1分钟开店", 'UTF-8'), font=qr_code_font, fill="#FF4500")
    image_buffer = BytesIO()
    image_background.save(image_buffer, format='PNG')
    image = base64.b64encode(image_buffer.getvalue()).decode()
    # image_background.show()
    return image

 

posted @ 2020-10-29 16:13  神经男孩  阅读(152)  评论(0)    收藏  举报