37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
from PIL import Image, ImageDraw, ImageFont
|
|
from textwrap import wrap
|
|
from io import BytesIO
|
|
from flask import send_file
|
|
|
|
def serve_pil_image(pil_img):
|
|
img_io = BytesIO()
|
|
pil_img.save(img_io, "PNG")
|
|
img_io.seek(0)
|
|
return send_file(img_io, mimetype='image/png')
|
|
|
|
def sign(text):
|
|
"d42551ff12586baf8f94c35fa3d1d19633e19259e11dc05cf20969f8bb393cf1"
|
|
wid = len(text)
|
|
tbg = Image.open("nokosample.png")
|
|
draw = ImageDraw.Draw(tbg)
|
|
wrappedtext = "\n".join(wrap(text, width = wid))
|
|
font = ImageFont.truetype("hr.otf", 720)
|
|
minimals = [abs(draw.textsize("\n".join(wrap(text, width = w, break_long_words = False)), font = font)[0]/draw.textsize("\n".join(wrap(text, width = w, break_long_words = False)), font = font)[1]-4/3) for w in range(1,wid)]
|
|
width = list(range(wid))[minimals.index(min(minimals))]
|
|
wrappedtext = "\n".join(wrap(text, width = width+1, break_long_words = False))
|
|
size = font.getsize(wrappedtext)#290 860 1220 1630
|
|
hig = 0
|
|
for line in wrappedtext.split("\n"):
|
|
hig += font.getsize(wrappedtext)[-1]
|
|
size = (size[0], int(hig*1.2))
|
|
bg = Image.new("RGBA", size)
|
|
d = ImageDraw.Draw(bg)
|
|
d.multiline_text((0, 0), wrappedtext, (101,67,33, 255), font = font, spacing = 70)
|
|
bg = bg.crop(bg.getbbox())
|
|
bg.thumbnail((950-50,810))
|
|
left = (950-50-bg.size[0])//2
|
|
upper = (810-bg.size[1])//2
|
|
tbg.paste(bg, (350+left, 900+upper), bg)
|
|
#bg.show()
|
|
return tbg
|