如何通过Python程序执行Photoshop脚本统计已打开图片数量?
Python调用Photoshop JSX脚本统计横竖版图片实现方案
前置说明
原有的JSX脚本默认通过弹窗输出结果,我们可以根据调用方式调整结果输出逻辑,方便Python接收统计数据。
方案1:Windows平台本地调用(最优方案,无临时文件)
依赖安装
执行命令安装pywin32库:
pip install pywin32
实现逻辑
直接通过COM接口连接已启动的Photoshop,传入JSX代码执行,直接获取返回的统计结果,无需生成中间文件。
完整Python代码
import win32com.client import json # 连接Photoshop应用 ps = win32com.client.Dispatch("Photoshop.Application") # 改造后的JSX代码,直接返回JSON格式的统计结果 jsx_code = """ s2t = stringIDToTypeID; var len = getPropertyDesc('application', p = 'numberOfDocuments').getInteger(s2t(p)), result = { v: 0, h: 0, total: len }; for (var i = 1; i <= len; i++) { var width = getPropertyDesc('document', p = 'width', i).getDouble(s2t(p)); var height = getPropertyDesc('document', p = 'height', i).getDouble(s2t(p)); result[width >= height ? 'h' : 'v']++ } function getPropertyDesc(target, property, idx) { target = s2t(target); (r = new ActionReference()).putProperty(s2t('property'), s2t(property)); idx ? r.putIndex(target, idx) : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum')); return executeActionGet(r); } return JSON.stringify(result); """ # 执行JSX代码并解析结果 result = json.loads(ps.DoJavaScript(jsx_code)) print(f"总打开图片数:{result['total']},竖版数量:{result['v']},横版数量:{result['h']}")
方案2:跨平台通用方案(支持Windows/Mac)
实现逻辑
- 将改造后的JSX脚本写入临时
.jsx文件 - 调用Photoshop主程序执行该脚本文件
- 脚本将统计结果写入临时文本文件,Python读取该文件获取结果
完整Python代码
import os import tempfile import subprocess import json # 填写你本地Photoshop的可执行文件路径,示例为Windows路径,Mac需修改为对应路径 PS_PATH = r"C:\Program Files\Adobe\Adobe Photoshop 2024\Photoshop.exe" # 生成临时文件路径 temp_jsx = tempfile.mktemp(suffix=".jsx") temp_result = tempfile.mktemp(suffix=".txt") # 改造后的JSX代码,将结果写入指定临时文件 jsx_code = f""" s2t = stringIDToTypeID; var len = getPropertyDesc('application', p = 'numberOfDocuments').getInteger(s2t(p)), result = {{ v: 0, h: 0, total: len }}; for (var i = 1; i <= len; i++) {{ var width = getPropertyDesc('document', p = 'width', i).getDouble(s2t(p)); var height = getPropertyDesc('document', p = 'height', i).getDouble(s2t(p)); result[width >= height ? 'h' : 'v']++ }} function getPropertyDesc(target, property, idx) {{ target = s2t(target); (r = new ActionReference()).putProperty(s2t('property'), s2t(property)); idx ? r.putIndex(target, idx) : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum')); return executeActionGet(r); }} // 写入结果到临时文件 var file = new File("{temp_result.replace(os.sep, '/')}"); file.open('w'); file.write(JSON.stringify(result)); file.close(); """ # 写入JSX临时文件 with open(temp_jsx, 'w', encoding='utf-8') as f: f.write(jsx_code) # 调用Photoshop执行脚本 subprocess.run([PS_PATH, temp_jsx], check=True) # 读取结果 with open(temp_result, 'r', encoding='utf-8') as f: result = json.load(f) print(f"总打开图片数:{result['total']},竖版数量:{result['v']},横版数量:{result['h']}") # 清理临时文件 os.unlink(temp_jsx) os.unlink(temp_result)
注意事项
- 运行代码前请确保Photoshop已经正常启动
- Mac系统下Photoshop可执行文件默认路径参考:
/Applications/Adobe Photoshop 2024/Adobe Photoshop 2024.app/Contents/MacOS/Adobe Photoshop 2024 - 请根据你安装的Photoshop版本调整路径中的版本号(如2023、2022等)
内容的提问来源于stack exchange,提问作者Saurabh Sharma
相关产品推荐
相关产品推荐

