psd 内图层是 cmyk 颜色模式,想要保留原本颜色进行导出,这边就用 ImageCms.profileToProfile 转换了下,但是,透明区域可以是白色或保持透明,这边是白色,却会多出一些不应该出现的颜色
# -- coding:UTF-8 --
from psd_tools import PSDImage
from psd_tools.constants import Resource
from PIL import ImageCms
import os
import configparser
config = configparser.ConfigParser()
config.read("config.ini", encoding="utf-8-sig")
def getConfigValue(section, key):
return config.get(section, key)
# 打开 PSD 文件
psd = PSDImage.open(getConfigValue("custom", 'psd_path'))
# 指定分组名称
group_name = '组 1'
output_group_folder = 'psd_export_png'
sub_group_name = '人物'
output_icc_profile = './color_icc/AdobeRGB1998.icc'
input_icc_profile = './color_icc/CoatedFOGRA39.icc'
# 遍历所有分组图层
for group in psd._layers:
if group.is_group() and group.name == group_name:
# 如果找到指定名称的分组图层,则导出其包含的图层
os.makedirs(output_group_folder, exist_ok=True)
for subgroup in group._layers:
if subgroup.is_group() and subgroup.name == sub_group_name:
for layer in subgroup._layers:
print(layer.name)
layer_image = layer.composite()
# 如果图像是 CMYK 模式,则进行颜色空间转换
if layer_image.mode == 'CMYK':
layer_image = ImageCms.profileToProfile(layer_image, './color_icc/CoatedFOGRA39.icc', './color_icc/AdobeRGB1998.icc', ImageCms.Intent.RELATIVE_COLORIMETRIC, "RGB")
layer_image.save(os.path.join(output_group_folder, f'{layer.name}.png'), format="PNG")
break