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

如何用Python调用Photoshop API或Pillow将图像蓝通道设为纯白?

解决方案:将法线贴图蓝通道填充为白色

下面提供两种实现方案,分别基于你当前使用的Photoshop Python API,以及更轻量的Pillow库。

方案1:使用Photoshop Python API实现

补全你已有的代码,通过PS API操作通道并填充白色:

import glob
import PySimpleGUI as sg
import photoshop.api as PS
from photoshop import Session

def Automate():
    # 遍历目标文件夹中的图片(建议指定后缀过滤非图片文件)
    for file_path in glob.glob(f"{values['TARGET_FOLDER']}/*.png"):
        with Session(version="23") as ps:
            # 打开图像文件
            doc = ps.app.open(file_path)
            # 获取蓝通道(PS通道顺序:复合通道[0]、红[1]、绿[2]、蓝[3])
            blue_channel = doc.channels[3]
            # 选中蓝通道
            blue_channel.selected = True
            # 设置前景色为纯白色
            white_color = PS.SolidColor()
            white_color.rgb.red = 255
            white_color.rgb.green = 255
            white_color.rgb.blue = 255
            ps.app.foregroundColor = white_color
            # 填充选中的蓝通道
            doc.selection.fill(ps.app.foregroundColor)
            # 保存并关闭文档
            doc.save()
            doc.close()
  • 替换glob.glob中的后缀(如*.png)为你需要处理的图像格式
  • Session会自动管理PS应用实例,无需单独创建PS.Application
  • 填充完成后记得保存并关闭文档,避免PS进程残留

方案2:使用Pillow库实现(无需Photoshop)

如果不需要依赖Photoshop,Pillow是更轻量的批量处理方案,无需启动PS即可完成操作:

首先安装Pillow:

pip install pillow

批量处理代码:

import glob
from PIL import Image

def batch_fill_blue_channel_white(target_folder):
    # 遍历目标文件夹内的图像文件(可调整后缀适配你的格式)
    for file_path in glob.glob(f"{target_folder}/*.png"):
        with Image.open(file_path) as img:
            # 分离RGB通道
            r_channel, g_channel, _ = img.split()
            # 创建纯白色的蓝通道(L模式为8位灰度,255对应白色)
            white_blue_channel = Image.new('L', img.size, 255)
            # 合并新的RGB通道
            processed_img = Image.merge('RGB', (r_channel, g_channel, white_blue_channel))
            # 保存处理后的图像(直接覆盖原文件,如需备份可修改保存路径)
            processed_img.save(file_path)
            print(f"已处理:{file_path}")

# 调用示例,替换为你的目标文件夹路径
batch_fill_blue_channel_white("你的目标文件夹路径")
  • 支持PNG、JPG、TGA等绝大多数常见图像格式
  • 无需安装Photoshop,适合批量处理大量文件
  • 若需要将蓝通道设为黑色,只需将Image.new中的255改为0即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 20:10:48