Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

替换图片 能替换成功,但是控制台输出错误 发生意外。', ('非法参数 - 参数 1\n- 缺少必需值' #348

Open
SleepEarly-1 opened this issue Jun 9, 2024 · 13 comments

Comments

@SleepEarly-1
Copy link

SleepEarly-1 commented Jun 9, 2024

下面是代码

from photoshop import Session

PSD_FILE = {
    "replace_images.psd": r"C:\Users\3KING\Desktop\test.psd",
    "red_100x200.png": r"C:\Users\3KING\Desktop\详情页\B.png"
}
LAYER_NAME = "B"  
``
with Session(PSD_FILE["replace_images.psd"], action="open") as ps:
    # 获取当前文档中的所有图层
    layers = ps.active_document.artLayers
    # 查找指定名称的图层
    target_layer = None
    for layer in layers:
        if layer.name == LAYER_NAME:
            target_layer = layer
            break

    if target_layer is None:
        print(f"图层 '{LAYER_NAME}' 未找到.")
    else:
        # 获取目标图层的边界
        bounds = target_layer.bounds
        print(f"当前图层 {target_layer.name}: {bounds}")

        # 定义要替换当前图层内容的新图像路径
        input_file = PSD_FILE["red_100x200.png"]
        # 获取“placedLayerReplaceContents”操作的ID
        replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
        # 创建一个动作描述符用于替换操作
        desc = ps.ActionDescriptor()
        # 获取“null”键的ID(用于在描述符中指定文件路径)
        idnull = ps.app.charIDToTypeID("null")
        # 在描述符中设置文件路径
        desc.putPath(idnull, input_file)
        # 执行操作,将当前图层的内容替换为新图像
        ps.app.executeAction(replace_contents, desc)

        # 获取更新后的目标图层(替换内容后)
        updated_layer = ps.active_document.activeLayer
        # 获取更新后图层的边界
        current_bounds = updated_layer.bounds
        # 计算原始图层的宽度和高度
        width = bounds[2] - bounds[0]
        height = bounds[3] - bounds[1]

        # 计算替换后图层的新宽度和高度
        current_width = current_bounds[2] - current_bounds[0]
        current_height = current_bounds[3] - current_bounds[1]
        # 计算调整尺寸的百分比以匹配原始图层的大小
        new_size = width / current_width * 100
        # 调整替换后图层的大小以匹配原始图层的大小,并保持其居中
        updated_layer.resize(new_size, new_size, ps.AnchorPosition.MiddleCenter)
        print(f"当前图层 {updated_layer.name}: {current_bounds}")

图片能正常替换成功,但是控制台会输出错误:

Traceback (most recent call last):
  File "D:\Python3.12.0\Lib\site-packages\comtypes\automation.py", line 879, in Invoke
    self.__com_Invoke(
_ctypes.COMError: (-2147352567, '发生意外。', (None, None, None, 0, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\PS_RPA\main.py", line 45, in <module>
    updated_layer = ps.active_document.activeLayer
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python3.12.0\Lib\site-packages\photoshop\api\_core.py", line 84, in __getattribute__
    return super().__getattribute__(item)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python3.12.0\Lib\site-packages\photoshop\api\_document.py", line 90, in activeLayer
    type_ = self.eval_javascript("app.activeDocument.activeLayer.typename")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python3.12.0\Lib\site-packages\photoshop\api\_core.py", line 218, in eval_javascript
    return executor.doJavaScript(javascript, Arguments, ExecutionMode)
           ^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python3.12.0\Lib\site-packages\comtypes\client\dynamic.py", line 137, in __getattr__
    raise err
  File "D:\Python3.12.0\Lib\site-packages\comtypes\client\dynamic.py", line 130, in __getattr__
    result = self._comobj.Invoke(dispid, _invkind=flags)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python3.12.0\Lib\site-packages\comtypes\automation.py", line 899, in Invoke
    raise COMError(hresult, text, details)
_ctypes.COMError: (-2147352567, '发生意外。', ('非法参数 - 参数 1\n- 缺少必需值', 'Adobe Photoshop', None, 0, -2147220262))
进程已结束,退出代码1

另外求作者能写一份中文文档救济一下国内的新手开发者。。跪谢!

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Title: Replace picture An accident occurred. ', ('Illegal parameter - parameter 1\n- missing required value'

@SleepEarly-1 SleepEarly-1 changed the title 替换图片 发生意外。', ('非法参数 - 参数 1\n- 缺少必需值' 替换图片 能替换成功,但是控制台输出错误 发生意外。', ('非法参数 - 参数 1\n- 缺少必需值' Jun 9, 2024
@SleepEarly-1
Copy link
Author

忘记说了使用的ps版本是2021 Adobe Photoshop
Adobe Photoshop 版本: 22.0.0 20201006.r.35 2020/10/06: 4587a1caa63 x64
操作系统:Windows 10 64 位
版本: 10 或更高版本 10.0.19041.3636

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Forgot to mention that the ps version used is 2021 Adobe Photoshop
Adobe Photoshop version: 22.0.0 20201006.r.35 2020/10/06: 4587a1caa63 x64
Operating system: Windows 10 64-bit
Version: 10 or higher 10.0.19041.3636

@loonghao
Copy link
Owner

你本地是否安装多个PS 的版本?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Do you have multiple versions of PS installed locally?

@SleepEarly-1
Copy link
Author

你本地是否安装多个PS 的版本?

只安装了一个

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Do you have multiple versions of PS installed locally?

Only one installed

@loonghao
Copy link
Owner

你本地是否安装多个PS 的版本?

只安装了一个

还有一个可能就是它在后台执行替换,你如果前台还在操作 ps 进行其他操作也容易触发这个问题。 当前用 COM 方式实现的API,出错不太好 debug

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Do you have multiple versions of PS installed locally?

Only one installed

Another possibility is that it performs replacement in the background. This problem can easily be triggered if you are still operating PS in the foreground and performing other operations. The API currently implemented in COM mode is not easy to debug when errors occur.

@SleepEarly-1
Copy link
Author

你本地是否安装多个PS 的版本?

只安装了一个

还有一个可能就是它在后台执行替换,你如果前台还在操作 ps 进行其他操作也容易触发这个问题。 当前用 COM 方式实现的API,出错不太好 debug

没有,执行代码的时候,PS已经退出了编辑模式,那只能先这样用着了,大佬有计划推出中文文档吗

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Do you have multiple versions of PS installed locally?

Only one is installed

Another possibility is that it performs replacement in the background. This problem can easily be triggered if you are still operating PS in the foreground and performing other operations. The API currently implemented in COM mode is not easy to debug when errors occur.

No, when executing the code, PS has exited the editing mode, so it can only be used like this for now. Do you have any plans to launch Chinese documents?

@loonghao
Copy link
Owner

关于文档,代码中的注释大部分都是来自于 官方 js 文档里面的描述,很多也解释的不是很清楚。 然后整个 ps 接口众多工程量挺大的,这个是个人业余项目,加上最近忙于工作上的其他事情,这个就暂时没有太多精力去补全了。 看看其他开源贡献者有没有时间可以帮忙贡献一下。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Regarding documentation, most of the comments in the code come from the descriptions in the official js documentation, and many of them are not explained very clearly. Then there are quite a lot of projects involved in the entire PS interface. This is a personal side project, and since I have been busy with other things at work recently, I don’t have much energy to complete it for the time being. See if other open source contributors have time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants