From 93e71022a5987ca898ca42a84e10cf3a1ddacd0a Mon Sep 17 00:00:00 2001 From: Kyomotoi <kyomotoiowo@gmail.com> Date: Tue, 5 Apr 2022 15:22:04 +0800 Subject: =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=E6=8F=92=E4=BB=B6:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATRI/plugins/polaroid/image_dealer.py | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ATRI/plugins/polaroid/image_dealer.py (limited to 'ATRI/plugins/polaroid/image_dealer.py') diff --git a/ATRI/plugins/polaroid/image_dealer.py b/ATRI/plugins/polaroid/image_dealer.py new file mode 100644 index 0000000..c1ab50b --- /dev/null +++ b/ATRI/plugins/polaroid/image_dealer.py @@ -0,0 +1,46 @@ +from random import choice +from pathlib import Path +from datetime import datetime + +from PIL import Image, ImageFont, ImageDraw + + +POLAROID_DIR = Path(".") / "data" / "database" / "polaroid" +TEMP_PATH = Path(".") / "data" / "temp" +POLAROID_DIR.mkdir(exist_ok=True) + + +def image_dealer(user_img: bytes, user_id): + user_p = str((TEMP_PATH / f"{user_id}.png").absolute()) + + with open(user_p, "wb") as w: + w.write(user_img) + + bg = Image.new("RGBA", (689, 1097), color=(0, 0, 0, 0)) + + pol_frame = Image.open(POLAROID_DIR / f"frame-{choice([0, 1])}.PNG").convert("RGBA") + user = Image.open(user_p).convert("RGBA").resize((800, 800), Image.ANTIALIAS) + + _, _, _, a = pol_frame.split() + + f_path = str(POLAROID_DIR / "font-0.ttf") + f_date = ImageFont.truetype(f_path, 100) # type: ignore + f_msg = ImageFont.truetype(f_path, 110) # type: ignore + + bg.paste(user, (0, 53)) + bg.paste(pol_frame, (0, 0), a) + + msg = f"今天辛苦了{choice(['!', '❤️', '✨'])}" + now = datetime.now().strftime("%m / %d") + + img = ImageDraw.Draw(bg) + img.text((25, 805), now, font=f_date, fill=(99, 184, 255)) + img.text( + (15, 915), + msg, + font=f_msg, + fill=(255, 100, 97), + ) + + bg.save(user_p) + return user_p -- cgit v1.2.3