mirror of
https://github.com/Murasame-Dev/McStatus-Img.git
synced 2025-12-16 21:27:58 +00:00
Add text with adaptive size
This commit is contained in:
parent
13255dcb55
commit
52f1a0d580
2 changed files with 32 additions and 10 deletions
|
|
@ -14,7 +14,10 @@ def create_background(input: bytes, width: int, height: int):
|
||||||
rwidth = int(width / height * h2)
|
rwidth = int(width / height * h2)
|
||||||
rheight = h2
|
rheight = h2
|
||||||
|
|
||||||
background = background.crop((midw - int(rwidth / 2), midh - int(rheight / 2), midw + int(rwidth / 2), midh + int(rheight / 2)))
|
background = background.crop((midw - int(rwidth / 2),
|
||||||
|
midh - int(rheight / 2),
|
||||||
|
midw + int(rwidth / 2),
|
||||||
|
midh + int(rheight / 2)))
|
||||||
background = background.resize((width, height), Image.Resampling.LANCZOS)
|
background = background.resize((width, height), Image.Resampling.LANCZOS)
|
||||||
|
|
||||||
blurred_background = background.filter(ImageFilter.GaussianBlur(radius=5))
|
blurred_background = background.filter(ImageFilter.GaussianBlur(radius=5))
|
||||||
|
|
@ -22,15 +25,21 @@ def create_background(input: bytes, width: int, height: int):
|
||||||
image = blurred_background
|
image = blurred_background
|
||||||
return image
|
return image
|
||||||
|
|
||||||
def draw_text_with_shadow(image: Image.Image, text: str, posx: int, posy: int):
|
def draw_text_with_shadow(image: Image.Image,
|
||||||
font_size = 14
|
text: str,
|
||||||
|
posx: int,
|
||||||
|
posy: int,
|
||||||
|
font_size: int):
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
font = ImageFont.truetype("./MiSans-Bold.ttf", font_size)
|
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')
|
||||||
|
|
||||||
def draw_motd_text_with_shadow(image: Image.Image, text: str, posx: int, posy: int):
|
def draw_motd_text_with_shadow(image: Image.Image,
|
||||||
font_size = 10
|
text: str,
|
||||||
|
posx: int,
|
||||||
|
posy: int,
|
||||||
|
font_size: int):
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
font = ImageFont.truetype("./MiSans-Bold.ttf", font_size)
|
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)
|
||||||
|
|
@ -44,9 +53,13 @@ def draw_motd_text_with_shadow(image: Image.Image, text: str, posx: int, posy: i
|
||||||
|
|
||||||
def create_image(background: bytes, icon: str | None, text_list: list[str], motd_list: list[str]):
|
def create_image(background: bytes, icon: str | None, text_list: list[str], motd_list: list[str]):
|
||||||
# 图片尺寸
|
# 图片尺寸
|
||||||
width, height = 600, 200
|
width, height = 1200, 400
|
||||||
if (len(text_list) + len(motd_list)) * 20 + 20 > height:
|
if (len(text_list) + len(motd_list)) * 20 + 20 > height:
|
||||||
height = len(text_list) * 20 + 20
|
height = (len(text_list) + len(motd_list)) * 20 + 20
|
||||||
|
else:
|
||||||
|
font_size = height // 12
|
||||||
|
if font_size * 20 > width - width // 2.5:
|
||||||
|
font_size = (width - width // 2.5) // 20
|
||||||
|
|
||||||
try:
|
try:
|
||||||
image = create_background(background, width, height)
|
image = create_background(background, width, height)
|
||||||
|
|
@ -70,9 +83,18 @@ def create_image(background: bytes, icon: str | None, text_list: list[str], motd
|
||||||
|
|
||||||
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
|
||||||
for i in range(text_list_size):
|
for i in range(text_list_size):
|
||||||
draw_text_with_shadow(image, text_list[i], width // 2.5, 10 + 20 * i)
|
draw_text_with_shadow(image,
|
||||||
|
text_list[i],
|
||||||
|
width // 2.5,
|
||||||
|
start_posy + font_size * 1.2 * i,
|
||||||
|
font_size)
|
||||||
for i in range(motd_list_size):
|
for i in range(motd_list_size):
|
||||||
draw_motd_text_with_shadow(image, motd_list[i], width // 2.5, 10 + 20 * (i + text_list_size))
|
draw_motd_text_with_shadow(image,
|
||||||
|
motd_list[i],
|
||||||
|
width // 2.5,
|
||||||
|
start_posy + font_size * 1.2 * (i + text_list_size),
|
||||||
|
int(font_size * 0.8))
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
|
||||||
2
main.py
2
main.py
|
|
@ -32,7 +32,7 @@ def generate_java_status_image(addr: str):
|
||||||
f"ip: {data["ip"]}",
|
f"ip: {data["ip"]}",
|
||||||
f"type: {data['type']}",
|
f"type: {data['type']}",
|
||||||
f"version: {data['version']}",
|
f"version: {data['version']}",
|
||||||
f"latency: {data['latency']} ms",
|
f"latency: {round(data['latency'], 2)} ms",
|
||||||
f"players: {data['players']['online']}/{data['players']['max']}",
|
f"players: {data['players']['online']}/{data['players']['max']}",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue