skip to Main Content

I have 40 Photoshop files and I need to copy the top layer on all of them and paste those layers into one Photoshop file. The second, background layer is the same in all files so I only need it once.

I have tried psd-tools but their documentation says they don’t support copying layers. I tried comtypes.client as suggested here but it just opened the files, I could not get it to copy and paste anything. Now I am trying to work with PIL (pillow) but I am not sure how to get it to copy and paste a layer rather than the whole image. I found this and from that I have the code below but it just returns an empty list and I am not sure where to go from there to get it to copy the layer I need.

import os
from PIL import Image, ImageSequence

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

From 40 files with two layers each I need to get one file with one background layer, and 40 pasted layers from the 40 files. If someone could help me finish the PIL code to copy layers that would be great but any other solutions are also highly appreciated.

2

Answers


  1. You can do this using one of Photoshop’s native scripting languages.

    Here’s how you would do it with 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();
            }
        }
    
    Login or Signup to reply.
  2. Here’s a sample code, copy/paste objects in Python, taken from https://github.com/lohriialo/photoshop-scripting-python/blob/master/CopyAndPaste.py

    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()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search