41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from PIL import Image
|
|
import os
|
|
import argparse
|
|
|
|
# 定义所需的图标尺寸
|
|
icon_sizes = {
|
|
'16x16': (16, 16),
|
|
'32x32': (32, 32),
|
|
'48x48': (48, 48),
|
|
'64x64': (64, 64),
|
|
'128x128': (128, 128),
|
|
'256x256': (256, 256),
|
|
'512x512': (512, 512)
|
|
}
|
|
|
|
def generate_icons(input_image_path, output_directory):
|
|
# 确保输出目录存在
|
|
os.makedirs(output_directory, exist_ok=True)
|
|
|
|
# 打开输入图像
|
|
with Image.open(input_image_path) as img:
|
|
for size_name, size in icon_sizes.items():
|
|
# 调整图像大小
|
|
icon = img.resize(size, Image.LANCZOS) # 使用LANCZOS替代ANTIALIAS
|
|
# 构建输出文件路径
|
|
output_path_png = os.path.join(output_directory, f'icon_{size_name}.png')
|
|
output_path_ico = os.path.join(output_directory, f'icon_{size_name}.ico')
|
|
# 保存PNG图标
|
|
icon.save(output_path_png, format='PNG')
|
|
print(f'生成PNG图标: {output_path_png}')
|
|
# 保存ICO图标
|
|
icon.save(output_path_ico, format='ICO')
|
|
print(f'生成ICO图标: {output_path_ico}')
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='生成多种尺寸的图标')
|
|
parser.add_argument('input_image', type=str, help='输入图像路径')
|
|
parser.add_argument('output_dir', type=str, help='输出目录')
|
|
args = parser.parse_args()
|
|
|
|
generate_icons(args.input_image, args.output_dir) |