convert-minecraft-server-icon/convert_icon.py
2025-07-31 08:01:58 +08:00

34 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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])