mirror of
https://github.com/Murasame-Dev/McStatus-Img.git
synced 2025-12-16 13:17:40 +00:00
add some custom settings.
This commit is contained in:
parent
fd80315876
commit
928e333e6b
2 changed files with 25 additions and 12 deletions
|
|
@ -31,9 +31,9 @@ def draw_text_with_shadow(image: Image.Image,
|
||||||
text: str,
|
text: str,
|
||||||
posx: int,
|
posx: int,
|
||||||
posy: int,
|
posy: int,
|
||||||
font_size: int):
|
font_size: int,
|
||||||
|
font):
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
font = ImageFont.truetype("./MiSans-Bold.ttf", font_size)
|
|
||||||
draw.text((posx + 2, posy + 2), text, font=font, fill='black')
|
draw.text((posx + 2, posy + 2), text, font=font, fill='black')
|
||||||
draw.text((posx, posy), text, font=font, fill='white')
|
draw.text((posx, posy), text, font=font, fill='white')
|
||||||
|
|
||||||
|
|
@ -41,9 +41,9 @@ def draw_motd_text_with_shadow(image: Image.Image,
|
||||||
text: str,
|
text: str,
|
||||||
posx: int,
|
posx: int,
|
||||||
posy: int,
|
posy: int,
|
||||||
font_size: int):
|
font_size: int,
|
||||||
|
font):
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
font = ImageFont.truetype("./MiSans-Bold.ttf", font_size)
|
|
||||||
w1, _, w2, _ = draw.textbbox((0, 0), text.strip(), font=font)
|
w1, _, w2, _ = draw.textbbox((0, 0), text.strip(), font=font)
|
||||||
weight = w2 - w1
|
weight = w2 - w1
|
||||||
motd_list = foramt_motd(text.strip(), weight)
|
motd_list = foramt_motd(text.strip(), weight)
|
||||||
|
|
@ -56,9 +56,17 @@ def draw_motd_text_with_shadow(image: Image.Image,
|
||||||
def create_image(background: bytes,
|
def create_image(background: bytes,
|
||||||
icon: str | None,
|
icon: str | None,
|
||||||
text_list: list[str],
|
text_list: list[str],
|
||||||
motd_list: list[str]):
|
motd_list: list[str],
|
||||||
|
font_url: str | None,
|
||||||
|
image_size: list[int]):
|
||||||
# 图片尺寸
|
# 图片尺寸
|
||||||
width, height = 1200, 400
|
if image_size == [0, 0] or image_size == []:
|
||||||
|
width = 1200
|
||||||
|
height = 400
|
||||||
|
else:
|
||||||
|
width = image_size[0]
|
||||||
|
height = image_size[1]
|
||||||
|
|
||||||
if (len(text_list) + len(motd_list)) * 20 + 20 > height:
|
if (len(text_list) + len(motd_list)) * 20 + 20 > height:
|
||||||
height = (len(text_list) + len(motd_list)) * 20 + 20
|
height = (len(text_list) + len(motd_list)) * 20 + 20
|
||||||
else:
|
else:
|
||||||
|
|
@ -74,7 +82,7 @@ def create_image(background: bytes,
|
||||||
# 添加半透明蒙版层以增强文字可读性
|
# 添加半透明蒙版层以增强文字可读性
|
||||||
overlay = Image.new('RGBA', (width, height), (0, 0, 0, 80)) # 半透明黑色蒙版
|
overlay = Image.new('RGBA', (width, height), (0, 0, 0, 80)) # 半透明黑色蒙版
|
||||||
image.paste(overlay, (0, 0), overlay)
|
image.paste(overlay, (0, 0), overlay)
|
||||||
|
|
||||||
if width // 2 > height:
|
if width // 2 > height:
|
||||||
small_size = int(height * 0.8)
|
small_size = int(height * 0.8)
|
||||||
else:
|
else:
|
||||||
|
|
@ -87,6 +95,12 @@ def create_image(background: bytes,
|
||||||
|
|
||||||
image.paste(small_image, (30, height // 2 - small_size // 2))
|
image.paste(small_image, (30, height // 2 - small_size // 2))
|
||||||
|
|
||||||
|
# 设置字体
|
||||||
|
if font_url == None:
|
||||||
|
font = ImageFont.load_default(font_size)
|
||||||
|
else:
|
||||||
|
font = ImageFont.truetype(font_url, font_size)
|
||||||
|
|
||||||
text_list_size = len(text_list)
|
text_list_size = len(text_list)
|
||||||
motd_list_size = len(motd_list)
|
motd_list_size = len(motd_list)
|
||||||
start_posy = height / 2 - (text_list_size + motd_list_size) / 2 * font_size * 1.2
|
start_posy = height / 2 - (text_list_size + motd_list_size) / 2 * font_size * 1.2
|
||||||
|
|
@ -95,12 +109,14 @@ def create_image(background: bytes,
|
||||||
motd_list[i],
|
motd_list[i],
|
||||||
width // 2.5,
|
width // 2.5,
|
||||||
start_posy + font_size * 1.2 * i,
|
start_posy + font_size * 1.2 * i,
|
||||||
int(font_size * 0.8))
|
int(font_size * 0.8),
|
||||||
|
font)
|
||||||
for i in range(text_list_size):
|
for i in range(text_list_size):
|
||||||
draw_text_with_shadow(image,
|
draw_text_with_shadow(image,
|
||||||
text_list[i],
|
text_list[i],
|
||||||
width // 2.5,
|
width // 2.5,
|
||||||
start_posy + font_size * 1.2 * (i + motd_list_size),
|
start_posy + font_size * 1.2 * (i + motd_list_size),
|
||||||
font_size)
|
font_size,
|
||||||
|
font)
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
from .get_background import download_image_with_httpx_auto_redirect
|
from .get_background import download_image_with_httpx_auto_redirect
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
# BACKGROUND_URL = "https://www.loliapi.com/acg/"
|
|
||||||
# DEFAULT_ICON = "./minecraft-creeper-face.png"
|
|
||||||
|
|
||||||
async def get_icon_image(url: str):
|
async def get_icon_image(url: str):
|
||||||
if url.startswith("http"):
|
if url.startswith("http"):
|
||||||
icon_data = await download_image_with_httpx_auto_redirect(url)
|
icon_data = await download_image_with_httpx_auto_redirect(url)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue