python
from PIL import Image
import os
def img_to_ico(pic_abs_path: str, target_size, ico_save_path: str = None) -> str:
"""
将JPG/PNG图片转换为ICO格式(支持指定尺寸)
:param pic_abs_path: 输入图片的绝对路径(支持.jpg/.jpeg/.png)
:param target_size: 目标ICO尺寸,如(64, 64)(宽高需一致,推荐16/32/64/128)
:param ico_save_path: 生成ICO的保存路径(默认与原图片同目录、同名,后缀改为.ico)
:return: 生成ICO文件的绝对路径
"""
# 1. 参数校验
# 校验输入图片格式
valid_ext = ('.jpg', '.jpeg', '.png')
pic_ext = os.path.splitext(pic_abs_path)[1].lower()
if pic_ext not in valid_ext:
raise ValueError(f"不支持的图片格式,仅支持{valid_ext}")
# 校验目标尺寸(宽高一致,且为正整数)
if len(target_size) != 2 or target_size[0] <= 0 or target_size[1] <= 0 or target_size[0] != target_size[1]:
raise ValueError("目标尺寸必须为宽高相等的正整数元组,如(64, 64)")
# 校验输入文件是否存在
if not os.path.exists(pic_abs_path):
raise FileNotFoundError(f"输入图片文件不存在:{pic_abs_path}")
# 2. 处理默认保存路径
if ico_save_path is None:
pic_dir = os.path.dirname(pic_abs_path)
pic_name = os.path.splitext(os.path.basename(pic_abs_path))[0]
ico_save_path = os.path.join(pic_dir, f"{pic_name}.ico")
# 3. 图片转换核心逻辑
try:
# 打开图片(PNG透明通道保留)
with Image.open(pic_abs_path) as img:
# 调整图片尺寸(保持原图比例,裁剪居中区域,避免变形)
img.thumbnail(target_size, Image.Resampling.LANCZOS) # 高质量缩放
if img.size != target_size:
# 居中裁剪到目标尺寸
left = (img.width - target_size[0]) / 2
top = (img.height - target_size[1]) / 2
right = left + target_size[0]
bottom = top + target_size[1]
img = img.crop((left, top, right, bottom))
# 转换为ICO格式并保存(ICO需为RGBA模式,保留透明)
if img.mode != 'RGBA':
img = img.convert('RGBA')
img.save(ico_save_path, format='ICO', sizes=[target_size])
# 4. 验证生成文件并返回路径
if os.path.exists(ico_save_path):
return os.path.abspath(ico_save_path)
else:
raise RuntimeError("ICO文件生成失败,未找到目标文件")
except Exception as e:
raise Exception(f"图片转换过程出错:{str(e)}") from e
try:
input_pic = r"C:\Users\1\Desktop\11111.png"
save_path = r"C:\Users\1\Desktop\11111.ico"
ico_path = img_to_ico(input_pic, (64,64), save_path)
print(f"ICO文件生成成功:{ico_path}")
except Exception as e:
print(f"失败:{e}")