时间: 2020-09-03 00:08:26 人气: 2310 评论: 0
新年到,先祝大家新年快乐:

怎么样,上面的这个小贺卡还不错吧?其实这个是通过Serverless架构自动生成的,测试地址:
新年贺卡,全都鼠于你 - 自豪的采用腾讯云Serverless架构

核心代码,就是Python通过pillow依赖,复制粘贴各种图片:
# -*- coding: utf8 -*- import qrcode, uuid, json, base64 from PIL import Image, ImageDraw, ImageFont def return_msg(error, msg): return_data = { "uuid": str(uuid.uuid1()), "error": error, "message": msg } print(return_data) return return_data def get_qrcode(url): qr = qrcode.QRCode( error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=8, border=1, ) # 传入数据 qr.add_data(url) qr.make(fit=True) # 生成二维码 img = qr.make_image() # 保存二维码 img.save('/tmp/qrcode.png') return Image.open('/tmp/qrcode.png').convert("RGBA") def get_input_str(input_str, font, draw, need_height=False): length = 0 new_str = "" max_height = [] for eve in input_str: width, height = draw.textsize(eve, font) max_height.append(height) length = length + width if length > 804 and eve != "\n": length = width new_str = new_str + "\n" + eve else: new_str = new_str + eve if need_height: if len(new_str.split("\n")) > 10: return False return (new_str, max(max_height) * len(new_str.split("\n")) if "\n" in new_str else max(max_height)) else: return new_str def do_watermark(temp_watermark_pic, watermark_pic_new_x, watermark_pic_new_y): for i in range(watermark_pic_new_x): for k in range(watermark_pic_new_y): color = temp_watermark_pic.getpixel((i, k)) if color != (255, 255, 255, 0): color = color[:-1] + (40,) temp_watermark_pic.putpixel((i, k), color) return temp_watermark_pic def main_handler(event, context): try: json_data = json.loads(event["body"]) frame_file = "./base/frame/%s.png" % (json_data["frame"]) title_file = "./base/word/%s.png" % (json_data["title"]) watermark_file = "./base/others/%s.png" % (json_data["watermark"]) font = ImageFont.truetype('./base/font/%s.ttf' % (json_data["font"]), 40) input_str = json_data["text"] from_str = json_data["from"] to_str = json_data["to"] symbol_file = "http://serverless.0duzhan.com/app/new_year_greeting_card/fu.html?id=%s" % (json_data["symbol"]) except Exception as e: print(e) return return_msg(True, "参数获取失败") if len(from_str) > 20: return return_msg(True, "落款昵称不可超过20字符") if len(to_str) > 20: return return_msg(True, "被赠与人昵称不可超过20字符") if len(input_str) > 200: return return_msg(True, "祝福内容不可以超过200字") try: temp_base_pic = Image.new('RGBA', (954, 1348), (255, 255, 255)) frame_pic = Image.open(frame_file).convert("RGBA").resize((954, 1348), Image.ANTIALIAS) title_pic = Image.open(title_file).convert("RGBA") watermark_pic = Image.open(watermark_file).convert("RGBA") title_pic_x, title_pic_y = title_pic.size title_pic_new_x = 310 title_pic_new_y = int(title_pic_y * title_pic_new_x / title_pic_x) title_pic = title_pic.resize((title_pic_new_x, title_pic_new_y), Image.ANTIALIAS) watermark_pic_x, watermark_pic_y = watermark_pic.size watermark_pic_new_x = 800 watermark_pic_new_y = int(watermark_pic_y * watermark_pic_new_x / watermark_pic_x) watermark_pic = watermark_pic.resize((watermark_pic_new_x, watermark_pic_new_y), Image.ANTIALIAS) temp_watermark_pic = Image.new('RGBA', (watermark_pic_new_x, watermark_pic_new_y), (255, 255, 255, 0)) temp_watermark_pic.paste(watermark_pic, (0, 0), watermark_pic) temp_watermark_pic = do_watermark(temp_watermark_pic, watermark_pic_new_x, watermark_pic_new_y) temp_base_pic.paste(frame_pic, (0, 0), frame_pic) temp_base_pic.paste(title_pic, (320, 30), title_pic) temp_base_pic.paste(temp_watermark_pic, (77, int((1348 - int(watermark_pic_new_y)) / 2)), temp_watermark_pic) temp_base_pic.paste(get_qrcode(symbol_file), (340, 950), get_qrcode(symbol_file)) draw = ImageDraw.Draw(temp_base_pic) draw.text((75, 300), get_input_str(to_str + ":", font, draw), (0, 0, 0), font=font) temp_data = get_input_str(input_str, font, draw, True) if not temp_data: return return_msg(True, "不要有太多的换行哦") input_str, content_height = temp_data draw.text((75, 400), input_str, (0, 0, 0), font=font) length_data = 850 for eve_ap in from_str: width, height = draw.textsize(eve_ap, font) length_data = length_data - width draw.text((length_data, content_height + 400), get_input_str(from_str, font, draw), (0, 0, 0), font=font) draw.text((415, 1240), get_input_str("扫码抽取新年福", font, draw), (0, 0, 0), font=ImageFont.truetype('./base/font/01.ttf', 20)) temp_base_pic.save("/tmp/test_output.png") with open("/tmp/test_output.png", "rb") as f: base64Data = str(base64.b64enco