将Photoshop文档转换为Numpy数组

2024-09-25 06:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python编写一些photoshop脚本。可在此处找到执行此操作的资源:https://github.com/loonghao/photoshop-scripting-python

主要是因为我想利用Numpy和其他一些ML库。我正在尝试访问photoshop文档,以便检查像素。PhotoshopAPI在这里有一个方法来实现这一点:Get the color of one pixel at Photoshop scripts

然而,它是超慢的。因此,我尝试将文档转换为Numpy数组,以加快处理速度。以下是我到目前为止所做的尝试。它的结果是ValueError: setting an array element with a sequence.我假设我可能需要先将它转换为图像?不确定。任何想法都很感激

# photoshop COM
from win32com.client import Dispatch, GetActiveObject
from comtypes.client import GetActiveObject, CreateObject
import numpy as np

app = GetActiveObject("Photoshop.Application")
doc = app.activeDocument

# set photoshop to use pixels and display no dialogue during automation
app.preferences.rulerUnits = 1  # from enum PsTypeUnits
app.preferences.typeUnits = 1  # from enum PsTypeUnits
app.displayDialogs = startDisplayDialogue

# I'd like to inspect the pixels of the document using Numpy
print(np.asarray(doc, dtype=np.float))

Tags: ofthetofrom文档importnumpyclient