You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从.ase(Adobe Swatch Exchange)文件提取调色板?

从ASE文件提取调色板并转为十六进制数组的Python实现

依赖安装

首先需要安装专门解析ASE文件的库asefile:

pip install asefile

完整实现脚本

import asefile

def ase_to_hex_palette(ase_path):
    hex_colors = []
    # 打开ASE文件
    with asefile.AseFile(ase_path) as af:
        for entry in af:
            # 只处理色板条目(跳过文件夹等非色板项)
            if entry.type == 'color':
                color_data = entry.color
                color_mode = entry.mode.lower()
                
                # 处理不同颜色模式,转为RGB
                if color_mode == 'rgb':
                    # ASE中RGB值是0-1的浮点数,转为0-255整数
                    r = int(round(color_data[0] * 255))
                    g = int(round(color_data[1] * 255))
                    b = int(round(color_data[2] * 255))
                elif color_mode == 'cmyk':
                    # CMYK转RGB(简化版,如需精确转换可使用colour-science库)
                    c, m, y, k = color_data
                    r = int(round(255 * (1 - c) * (1 - k)))
                    g = int(round(255 * (1 - m) * (1 - k)))
                    b = int(round(255 * (1 - y) * (1 - k)))
                elif color_mode == 'lab':
                    # LAB转RGB需要依赖colour库,这里先跳过,如需可添加依赖
                    continue
                else:
                    # 不支持的颜色模式跳过
                    continue
                
                # 转为十六进制格式,确保两位补零,大写输出
                hex_color = f"#{r:02X}{g:02X}{b:02X}"
                hex_colors.append(hex_color)
    return hex_colors

# 示例使用
if __name__ == "__main__":
    palette = ase_to_hex_palette("your_palette.ase")
    print("提取的十六进制调色板:")
    print(palette)

关键说明

  • 色板过滤:ASE文件可能包含文件夹分组,代码中通过entry.type == 'color'只提取色板条目
  • 颜色模式转换:
    • RGB模式直接将0-1浮点数转换为0-255整数
    • CMYK提供了简化转换逻辑,如需更精确的色彩转换,可安装colour-science库调用专业转换函数
    • LAB等其他模式如需支持,可扩展对应转换逻辑
  • 十六进制格式化:使用f"#{r:02X}{g:02X}{b:02X}"确保每个颜色分量为两位十六进制,大写输出(如需小写可替换X为x)

测试

将上述脚本保存为ase_extractor.py,替换your_palette.ase为你的ASE文件路径,运行脚本即可得到十六进制颜色数组。

内容的提问来源于stack exchange,提问作者NameNotFound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 00:49:52