V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
penll
V2EX  ›  Python

pillow 导出 psd 内图层的图片,透明区域本应该是白色却多出一些颜色,请问如何解决

  •  
  •   penll · 2023-04-06 17:27:48 +08:00 · 738 次点击
    这是一个创建于 401 天前的主题,其中的信息可能已经有所发展或是发生改变。

    场景:

    psd 内图层是 cmyk 颜色模式,想要保留原本颜色进行导出,这边就用 ImageCms.profileToProfile 转换了下,但是,透明区域可以是白色或保持透明,这边是白色,却会多出一些不应该出现的颜色

    错误图片,红色圈部分为多出颜色:

    err_img

    代码如下:

    
    # -- 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
    
    penll
        1
    penll  
    OP
       2023-04-07 08:48:47 +08:00
    有没有懂的大佬,帮忙看下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   930 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:50 · PVG 05:50 · LAX 14:50 · JFK 17:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.