• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with script to extract subjects and paste into new document

Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

I had chatgpt write a script to take all the open documents, select the subjects and paste them into a new composite image. Each open image has only one subject on a black background. The object selection tool manages to extract it perfectly when I go through one image at a time, but I have a lot of images, so I need to make it work as a script. When I run the following script I get an error that says: Could not complete the command because the selected area is empty. Any suggestions on how to fix this? 

 

// Photoshop script to extract subjects from multiple open images and paste them into a new composite file
#target photoshop

// Make sure there are open documents
if (app.documents.length > 0) {
    // Create a new document for the composite
    var newDoc = app.documents.add(3000, 3000, 300, "Composite Image", NewDocumentMode.RGB);

    // Iterate through all open documents
    for (var i = 0; i < app.documents.length; i++) {
        var currentDoc = app.documents[i];
        app.activeDocument = currentDoc;

        // Make a selection of the subject
        try {
            // Use Select Subject or fallback to manual selection if Select Subject fails
            currentDoc.selection.selectSubject();
        } catch (e) {
            alert("Failed to select subject in document: " + currentDoc.name);
            continue;
        }

        // Check if selection is empty
        try {
            var bounds = currentDoc.selection.bounds;
        } catch (e) {
            alert("No subject found in document: " + currentDoc.name);
            continue;
        }

        // Copy the selection
        currentDoc.selection.copy();

        // Paste into the new composite document
        app.activeDocument = newDoc;
        newDoc.paste();

        // Move the pasted layer to arrange it better (optional)
        var newLayer = newDoc.activeLayer;
        newLayer.translate(i * 50, i * 50); // Offset each pasted subject
    }

    alert("Extraction and composite complete!");
} else {
    alert("No open documents found. Please open the photos you want to process.");
}

/*
Note:
- This script attempts to use the "Select Subject" feature and checks if the selection is valid before proceeding.
- The new composite document is created with dimensions of 3000x3000 pixels at 300 DPI. You may adjust this as needed.
- Each pasted layer is slightly offset for visibility; you can modify the `translate` function for better arrangement.
*/
 
4wkc3ugubv.png
TOPICS
Actions and scripting , Windows

Views

309

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 06, 2024 Dec 06, 2024

This is junk code:

 

// Use Select Subject or fallback to manual selection if Select Subject fails
currentDoc.selection.selectSubject();

 

Try this:

 

// Use Select Subject or fallback to manual selection if Select Subject fails
executeAction(stringIDToTypeID("autoCutout"), undefined, DialogModes.NO);

 

For more accurate, but slower selections, set your preferences/settings to use cloud processing. 

Votes

Translate

Translate
Community Expert , Dec 07, 2024 Dec 07, 2024

This version should avoid the double-placement: 

// Make sure there are open documents
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theDocs = app.documents;
var theNumberOfLayers = app.documents.length;
// Create a new document for the composite
var newDoc = app.documents.add(3000, 3000, 300, "Composite Image", NewDocumentMode.RGB);
// Iterate through all open documents
for (var i = 0; i < theNumberOfLayers; i
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Can you provide links to two or more sample images exactly the same as you use for production? You can redact the faces or make them half or quarter size for testing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

How did you create the code you originally posted? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

quote

How did you create the code you originally posted? 


By @c.pfaffenbichler

 

@Nerosetsfire wrote: “I had chatgpt write a script"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

quote
quote

How did you create the code you originally posted? 


By @c.pfaffenbichler

 

@Nerosetsfire wrote: “I had chatgpt write a script"


By @Stephen Marsh

I do apologize, I had read the original post sloppily. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Where did you get the line 

currentDoc.selection.selectSubject();

from? 

Could it be a generative AI hallucination? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

This is junk code:

 

// Use Select Subject or fallback to manual selection if Select Subject fails
currentDoc.selection.selectSubject();

 

Try this:

 

// Use Select Subject or fallback to manual selection if Select Subject fails
executeAction(stringIDToTypeID("autoCutout"), undefined, DialogModes.NO);

 

For more accurate, but slower selections, set your preferences/settings to use cloud processing. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

still gave me the same error 😞

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Seems to work here with the correction of the garbage-line of code @Stephen Marsh recommended. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Is this what the code should look like?

Code_6DBQhyJvXu.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

I have no doubt that I'm doing something wrong, but it's not working, here is the script with the suggested code

 

// Photoshop script to extract subjects from multiple open images and paste them into a new composite file
#target photoshop

// Make sure there are open documents
if (app.documents.length > 0) {
    // Create a new document for the composite
    var newDoc = app.documents.add(3000, 3000, 300, "Composite Image", NewDocumentMode.RGB);

    // Iterate through all open documents
    for (var i = 0; i < app.documents.length; i++) {
        var currentDoc = app.documents[i];
        app.activeDocument = currentDoc;

        // Make a selection of the subject
        try {
            // Use Select Subject or fallback to manual selection if Select Subject fails
executeAction(stringIDToTypeID("autoCutout"), undefined, DialogModes.NO);
        } catch (e) {
            alert("Failed to select subject in document: " + currentDoc.name);
            continue;
        }

        // Check if selection is empty
        try {
            var bounds = currentDoc.selection.bounds;
        } catch (e) {
            alert("No subject found in document: " + currentDoc.name);
            continue;
        }

        // Copy the selection
        currentDoc.selection.copy();

        // Paste into the new composite document
        app.activeDocument = newDoc;
        newDoc.paste();

        // Move the pasted layer to arrange it better (optional)
        var newLayer = newDoc.activeLayer;
        newLayer.translate(i * 50, i * 50); // Offset each pasted subject
    }

    alert("Extraction and composite complete!");
} else {
    alert("No open documents found. Please open the photos you want to process.");
}

/*
Note:
- This script attempts to use the "Select Subject" feature and checks if the selection is valid before proceeding.
- The new composite document is created with dimensions of 3000x3000 pixels at 300 DPI. You may adjust this as needed.
- Each pasted layer is slightly offset for visibility; you can modify the `translate` function for better arrangement.
*/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Works here (though one image appears to be placed twice). 

Have you restarted Photoshop and openened a couple of images? 

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Yes I restarted Photoshop and Lightroom. I'm selecting a few photos in Lightroom and then opening them in Photoshop that's how I'm getting them into there here is a screenshot it seems to work for the first photo and then flakes out for the rest 

LkIb5x6PUk.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

@Nerosetsfire 

 

Your updated code works for me (although it pastes the last image twice, so two source docs results in three layers).

 

Please use the </> button in the forum interface to paste code.

 

Note, the script assumes that the app's preferences are set to px as the ruler unit, otherwise if using inches you would have a 3000 inch square composite doc.  :]

 

After paste into the composite, you may wish to add this line:

 

executeAction(stringIDToTypeID("removeBlackMatte"), undefined, DialogModes.NO);

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

quote

Your updated code works for me (although it pastes the last image twice, so two source docs results in three layers).

The for-clause works based on the number of documents, so that means the new document is counted, too. 

It might be better to define an Array of the open documents before creating the new image and work off of that. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Okay thanks what appear to be screwing it up is that I had mine set to inches and not pixels as soon as I change that everything worked much appreciated

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2024 Dec 07, 2024

Copy link to clipboard

Copied


@Nerosetsfire wrote:

Okay thanks what appear to be screwing it up is that I had mine set to inches and not pixels as soon as I change that everything worked much appreciated


 

Yes, this can be accounted for in the code:

 

// Create a new document for the composite
    var newDoc = app.documents.add(UnitValue(3000, "px"), UnitValue(3000, "px"), 300, "Composite Image", NewDocumentMode.RGB);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2024 Dec 07, 2024

Copy link to clipboard

Copied

This version should avoid the double-placement: 

// Make sure there are open documents
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theDocs = app.documents;
var theNumberOfLayers = app.documents.length;
// Create a new document for the composite
var newDoc = app.documents.add(3000, 3000, 300, "Composite Image", NewDocumentMode.RGB);
// Iterate through all open documents
for (var i = 0; i < theNumberOfLayers; i++) {
var currentDoc = app.documents[i];
app.activeDocument = currentDoc;
// Make a selection of the subject
try {
// Use Select Subject or fallback to manual selection if Select Subject fails
executeAction(stringIDToTypeID("autoCutout"), undefined, DialogModes.NO);
} catch (e) {
alert("Failed to select subject in document: " + currentDoc.name);
continue;
}
// Check if selection is empty
try {
var bounds = currentDoc.selection.bounds;
} catch (e) {
alert("No subject found in document: " + currentDoc.name);
continue;
};
// Copy the selection
currentDoc.selection.copy();
// Paste into the new composite document
app.activeDocument = newDoc;
newDoc.paste();
// Move the pasted layer to arrange it better (optional)
var newLayer = newDoc.activeLayer;
newLayer.name = currentDoc.name;
newLayer.translate(i * 50, i * 50); // Offset each pasted subject
}
alert("Extraction and composite complete!");
} else {
alert("No open documents found. Please open the photos you want to process.");
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2024 Dec 07, 2024

Copy link to clipboard

Copied

Well that's incredibly trying to do thank you very much for doing this to have your appreciate it and I have no idea and no desire how to learn to program for right script I already have enough time. Have a great day!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2024 Dec 08, 2024

Copy link to clipboard

Copied

LATEST

The previous Script should also name the Layers according to the images’ names. 

 

A point on technique: 

In my experience »Select Subject« rarely delivers a result that doesn’t need to be amended at least somewhat. 

 

So if the results should not meet your expectations/needs it might be better to duplicate the individual images in their entirety, use »Select Subject« in the receiving document and apply the Selection as a Layer Mask.  

So one can edit the Layer Mask if/as necessary. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines