This commit is contained in:
2025-09-30 15:18:16 +08:00
39 changed files with 2700 additions and 109 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.0 KiB

View File

@ -1,9 +1,11 @@
from io import BytesIO
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from konabot.plugins.roll_dice.base.path import ASSETS
from konabot.common.path import ASSETS_PATH, FONTS_PATH
def text_to_transparent_image(text, font_size=40, padding=0, text_color=(0, 0, 0)):
"""
@ -13,15 +15,7 @@ def text_to_transparent_image(text, font_size=40, padding=0, text_color=(0, 0, 0
temp_image = Image.new('RGB', (1, 1), (255, 255, 255))
temp_draw = ImageDraw.Draw(temp_image)
font = ImageFont.truetype(ASSETS / "montserrat.otf", font_size)
# try:
# font = ImageFont.truetype(ASSETS / "montserrat.otf", font_size)
# except:
# try:
# font = ImageFont.truetype("arial.ttf", font_size)
# except:
# # 如果系统字体不可用,使用默认字体
# font = ImageFont.load_default()
font = ImageFont.truetype(FONTS_PATH / "montserrat.otf", font_size)
# 获取文本边界框
bbox = temp_draw.textbbox((0, 0), text, font=font)
@ -29,8 +23,8 @@ def text_to_transparent_image(text, font_size=40, padding=0, text_color=(0, 0, 0
text_height = bbox[3] - bbox[1]
# 计算图像大小(文本大小 + 内边距)
image_width = text_width + 2 * padding
image_height = text_height + 2 * padding
image_width = int(text_width + 2 * padding)
image_height = int(text_height + 2 * padding)
# 创建RGBA模式的空白图像带透明通道
image = Image.new('RGBA', (image_width, image_height), (0, 0, 0, 0))
@ -314,7 +308,7 @@ async def generate_dice_image(number: str) -> BytesIO:
# 如果文本太长,直接返回金箍棒
if(len(text) > 50):
output = BytesIO()
push_image = Image.open(ASSETS / "stick.png")
push_image = Image.open(ASSETS_PATH / "stick.png")
push_image.save(output,format='PNG')
output.seek(0)
return output
@ -340,7 +334,7 @@ async def generate_dice_image(number: str) -> BytesIO:
move_distance = (up_direction[0] * (stretch_k - 1), up_direction[1] * (stretch_k - 1))
# 加载背景图像,保留透明通道
background = cv2.imread(ASSETS / "template.png", cv2.IMREAD_UNCHANGED)
background = cv2.imread(ASSETS_PATH / "template.png", cv2.IMREAD_UNCHANGED)
height, width = background.shape[:2]
@ -369,7 +363,7 @@ async def generate_dice_image(number: str) -> BytesIO:
pil_final = Image.fromarray(final_image_simple)
# 导入一系列图像
images = [Image.open(ASSETS / f"{i}.png") for i in range(1, 12)]
images: list[Image.Image] = [Image.open(ASSETS_PATH / "img" / "dice" / f"{i}.png") for i in range(1, 12)]
images.append(pil_final)
frame_durations = [100] * (len(images) - 1) + [100000]
# 将导入的图像尺寸扩展为和 pil_final 相同的大小,随帧数进行扩展,然后不放大的情况下放在最中间