python - 将图像文件作为图层加载到 Photoshop

标签 python python-3.x photoshop

我有一个现有的 PSD 文件,其中包含两个图像。我的目标是将其中一个图像更改为位于同一文件夹中的另一图像。我检查了其他问题,发现使用Python可以通过不同的方式实现相同的结果,但是我找不到我想要的实现的解决方案。 (我想通过Photoshop的COM接口(interface)来做到这一点)

实际上,我可以访问图层并设置其属性,但无法更改源图像/对象。

import win32com.client
import os

# opens ps
psApp = win32com.client.Dispatch("Photoshop.Application")
# opens file
psApp.Open(r"F:\dev\ae\f1.psd") 

# new image to add
img1 = r"F:\dev\ae\img1.png"

doc = psApp.Application.ActiveDocument

# new blank layer
doc.ArtLayers.Add()

# get layer to change
layer = doc.ArtLayers[1]        

Adobe has scripting guides对于各种语言,我发现Python中的大多数方法和属性名称都是基于VBScript版本,但我什至无法找出解决方案with the docs 。当我在 ArtLayers 上调用 Add() 方法时,它会创建一个空白图层,但我没有找到其他方法对图像/智能对象执行相同操作。

最佳答案

嘿,我一直在寻找同样的东西,在深入研究您找到的文档中的示例代码后,这就是我找到的解决方法。

psApp.Load(img1)
psApp.ActiveDocument.Selection.SelectAll()
psApp.ActiveDocument.Selection.Copy()
psApp.ActiveDocument.Close()
psApp.ActiveDocument.Paste()

我的意思是这有点愚蠢,但它确实有效。如果不是最后一个文档,您可能需要在粘贴之前重新选择您所在的文档。

关于python - 将图像文件作为图层加载到 Photoshop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58943987/

相关文章:

python - numpy 数据类型的混淆

python - 如何保持行和列的大小与 PyQt5 中的网格布局相同?

python - 如何在 Python3 中设置文件权限?

javascript - 自动化批处理脚本 - 在 Photoshop 中将文件名转换为文本

python - 从python中的特定索引值排序列表

python - 在 Python (Flask) 中将 GET 更改为 POST

python - DeprecationWarning : QDesktopWidget. availableGeometry(int screen) const 已弃用 centerPoint = QDesktopWidget().availableGeometry().center()

python - 无法使用 getattr() 函数解析 bs4 src 属性

flash - 如何使用 actionscript 3 读取/解析曲线调整 (*.acv) 文件

opengl-es - Photoshop 混合模式到 OpenGL ES(无需着色器)