46 lines
No EOL
1.1 KiB
Python
46 lines
No EOL
1.1 KiB
Python
import time
|
|
import hashlib
|
|
import secrets
|
|
import os
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
from config.config import TMP
|
|
|
|
# variables
|
|
PROMA_WIDTH = 600
|
|
PROMA_HEIGHT = 600
|
|
PROMA_FONTSIZE = 40
|
|
PROMA_FONT = "./resource/OpenSans-Regular.ttf"
|
|
|
|
# generate
|
|
# return value : filename
|
|
# output to file
|
|
def gen(context:dict) -> str | None:
|
|
# data preparation
|
|
content = context["content"]["text"]
|
|
|
|
# generate image
|
|
img = Image.new(mode="RGB",
|
|
size=(PROMA_WIDTH, PROMA_HEIGHT),
|
|
color=(255, 255, 255))
|
|
|
|
font = ImageFont.truetype(PROMA_FONT, PROMA_FONTSIZE, encoding='utf-8')
|
|
|
|
draw:ImageDraw.ImageDraw = ImageDraw.Draw(img)
|
|
draw.text(xy=(0, 0),
|
|
text=content,
|
|
font=font,
|
|
fill=(0, 0, 0))
|
|
|
|
# save
|
|
filename = TMP + hashlib.sha512( (str(time.time()) + secrets.token_urlsafe(nbytes=24)).encode() ).hexdigest() + ".jpg"
|
|
img.save(filename)
|
|
filename = os.path.abspath(filename)
|
|
|
|
return filename
|
|
|
|
|
|
# 文案生成
|
|
def gen_caption(context:dict) -> str:
|
|
return context["content"]["text"] |