使用Python复制和粘贴Photoshop图层

时间:2019-06-11 14:01:28

标签: python-3.x python-imaging-library photoshop

我有40个Photoshop文件,我需要复制所有文件的顶层并将这些层粘贴到一个Photoshop文件中。第二个背景层在所有文件中都相同,因此我只需要一次。

我尝试过psd-tools,但是他们的文档说它们不支持复制图层。我按照建议的here尝试了comtypes.client,但是它只是打开了文件,我无法复制和粘贴任何内容。现在,我尝试使用PIL(枕头),但不确定如何复制和粘贴图层而不是整个图像。我找到了this,并从中得到了下面的代码,但是它只返回一个空列表,而且我不确定从那里到哪里去复制它所需的图层。

import os
from PIL import Image, ImageSequence

image = Image.open("Baroccip1 - Drawing 1.psd")
layers = [frame.copy() for frame in ImageSequence.Iterator(image)]

我需要从40个文件中分为两层,每个文件中需要一个背景层,并从40个文件中获得40个粘贴层。如果有人可以帮助我完成PIL代码以复制图层,那将是不错的选择,但其他解决方案也将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

您可以使用Photoshop的一种本机脚本语言来执行此操作。

使用Javascript的方法如下:

    dupeLayer();
    function dupeLayer() {
        //Make sure your destination document is the only open document before running
        //Select source folder and filter for PSD files
        var psdFolder = Folder.selectDialog("Choose source PSD folder");
        var workFiles = psdFolder.getFiles("*.psd");
        //Loop through all PSDs in psdFolder
        for (i = 0; i < workFiles.length; i++) {
            app.open(new File(workFiles[i]));
            //Duplicate topmost layer to first open document
            app.documents[1].layers[0].duplicate(app.documents[0],ElementPlacement.PLACEATBEGINNING);
            app.documents[1].close();
        }
    }

答案 1 :(得分:0)

这是一个示例代码,使用https://github.com/lohriialo/photoshop-scripting-python/blob/master/CopyAndPaste.py

的Python复制/粘贴对象。
psReplaceSelection = 1
selection_area = ((0, 0), (x2, 0), (x2, y2), (0, y2))
sourceDoc.Selection.Select(selection_area, psReplaceSelection, 0, False)
sourceDoc.Selection.Copy()

destinationDoc.Paste()