python --生成ico图标

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}")
相关推荐
m0_5649149215 小时前
Altium Designer,AD如何修改原理图右下角图纸标题栏?如何自定义标题栏?自定义原理图模版的使用方法
java·服务器·前端
飞升不如收破烂~15 小时前
# Spring Boot 跨域请求未到达后端问题排查记录
java·spring boot·后端
AllData公司负责人15 小时前
【亲测好用】数据集成管理能力演示
java·大数据·数据库·开源
一路向阳~负责的男人15 小时前
PyTorch / CUDA 是什么?它们的关系?
人工智能·pytorch·python
阿蒙Amon15 小时前
C#每日面试题-值传递和引用传递的区别
java·面试·c#
aloha_78915 小时前
乐信面试准备
java·spring boot·python·面试·职场和发展·maven
火云洞红孩儿16 小时前
零基础:100个小案例玩转Python软件开发!第六节:英语教学软件
开发语言·python
2401_8414956416 小时前
深度卷积生成对抗网络(DCGAN)
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·深度卷积生成对抗网络
Knight_AL16 小时前
Spring Boot 多模块项目中优雅实现自动配置(基于 AutoConfiguration.imports)
java·spring boot·mybatis
忧郁的橙子.16 小时前
26期_01_Pyhton函数进阶
python