添加 convert_icon.py

This commit is contained in:
mei 2025-07-31 08:01:58 +08:00
commit f704cb4910

33
convert_icon.py Normal file
View File

@ -0,0 +1,33 @@
from PIL import Image
import sys
import os
def convert_to_minecraft_icon(input_path, output_path="server-icon.png"):
try:
with Image.open(input_path) as img:
# 将图片转为RGBA模式确保透明度兼容性
img = img.convert("RGBA")
# 获取最小边长,中心裁剪为正方形
min_side = min(img.size)
left = (img.width - min_side) // 2
top = (img.height - min_side) // 2
right = left + min_side
bottom = top + min_side
img_cropped = img.crop((left, top, right, bottom))
# 缩放为64x64像素
img_resized = img_cropped.resize((64, 64), Image.LANCZOS)
# 保存为PNG格式
img_resized.save(output_path, format="PNG")
print(f"✔ 成功生成 Minecraft 服务器图标: {output_path}")
except Exception as e:
print(f"❌ 处理失败: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python convert_icon.py <输入图片路径>")
else:
convert_to_minecraft_icon(sys.argv[1])