60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from PIL import Image
|
|
import os
|
|
|
|
# 图片路径
|
|
input_path = r"e:\project\flutter\f3\flutter_application_2\assets\IMG_20260402_172628.png"
|
|
output_dir = r"e:\project\flutter\f3\flutter_application_2\assets"
|
|
|
|
# 打开图片
|
|
img = Image.open(input_path)
|
|
print(f"原始图片尺寸: {img.size}")
|
|
print(f"原始图片模式: {img.mode}")
|
|
|
|
# 定义需要的尺寸(应用图标和启动图常见尺寸)
|
|
sizes = [
|
|
(1024, 1024), # iOS/macOS 主图标
|
|
(512, 512), # 大图标
|
|
(256, 256), # 中图标
|
|
(128, 128), # 小图标
|
|
(64, 64), # 更小图标
|
|
(192, 192), # Android mipmap-xxhdpi
|
|
(144, 144), # Android mipmap-xxxhdpi (96x1.5)
|
|
(96, 96), # Android mipmap-xhdpi
|
|
(72, 72), # Android mipmap-hdpi
|
|
(48, 48), # Android mipmap-mdpi
|
|
(216, 216), # HarmonyOS icon_xlarge
|
|
(144, 144), # HarmonyOS icon_large
|
|
(96, 96), # HarmonyOS icon
|
|
(72, 72), # HarmonyOS icon_small
|
|
]
|
|
|
|
# 裁剪为正方形(居中裁剪)
|
|
width, height = img.size
|
|
min_size = min(width, height)
|
|
left = (width - min_size) // 2
|
|
top = (height - min_size) // 2
|
|
right = left + min_size
|
|
bottom = top + min_size
|
|
square_img = img.crop((left, top, right, bottom))
|
|
print(f"裁剪后正方形尺寸: {square_img.size}")
|
|
|
|
# 保存正方形原图
|
|
square_path = os.path.join(output_dir, "app_icon_square.png")
|
|
square_img.save(square_path, "PNG")
|
|
print(f"保存正方形原图: {square_path}")
|
|
|
|
# 生成各种尺寸
|
|
for size in sizes:
|
|
resized = square_img.resize(size, Image.Resampling.LANCZOS)
|
|
output_path = os.path.join(output_dir, f"app_icon_{size[0]}x{size[1]}.png")
|
|
resized.save(output_path, "PNG")
|
|
print(f"保存: {output_path}")
|
|
|
|
# 保存为项目默认图标
|
|
default_icon_path = os.path.join(output_dir, "app_icon_default.png")
|
|
default_size = (512, 512)
|
|
default_img = square_img.resize(default_size, Image.Resampling.LANCZOS)
|
|
default_img.save(default_icon_path, "PNG")
|
|
print(f"\n保存项目默认图标: {default_icon_path}")
|
|
print("完成!")
|