Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/maimai/libraries/image.py

53 lines
1.6 KiB
Python
Raw Normal View History

2021-10-12 15:02:36 +00:00
import base64
from io import BytesIO
from PIL import ImageFont, ImageDraw, Image
path = 'assets/maimai/static/high_eq_image.png'
fontpath = "assets/maimai/static/msyh.ttc"
def draw_text(img_pil, text, offset_x):
draw = ImageDraw.Draw(img_pil)
font = ImageFont.truetype(fontpath, 48)
width, height = draw.textsize(text, font)
x = 5
if width > 390:
font = ImageFont.truetype(fontpath, int(390 * 48 / width))
width, height = draw.textsize(text, font)
else:
x = int((400 - width) / 2)
draw.rectangle((x + offset_x - 2, 360, x + 2 + width + offset_x, 360 + height * 1.2), fill=(0, 0, 0, 255))
draw.text((x + offset_x, 360), text, font=font, fill=(255, 255, 255, 255))
def text_to_image(text):
font = ImageFont.truetype(fontpath, 24)
padding = 10
margin = 4
text_list = text.split('\n')
max_width = 0
2023-05-27 09:08:23 +00:00
total_height = 0
for text_line in text_list:
w, h = font.getsize(text_line)
2021-10-12 15:02:36 +00:00
max_width = max(max_width, w)
2023-05-27 09:08:23 +00:00
total_height += h + margin
2021-10-12 15:02:36 +00:00
wa = max_width + padding * 2
2023-05-27 09:08:23 +00:00
ha = total_height + padding * 2
image = Image.new('RGB', (wa, ha), color=(255, 255, 255))
draw = ImageDraw.Draw(image)
current_height = padding
for text_line in text_list:
w, h = font.getsize(text_line)
draw.text((padding, current_height), text_line, font=font, fill=(0, 0, 0))
current_height += h + margin
return image
2021-10-12 15:02:36 +00:00
def image_to_base64(img, format='PNG'):
output_buffer = BytesIO()
img.save(output_buffer, format)
byte_data = output_buffer.getvalue()
base64_str = base64.b64encode(byte_data)
return base64_str