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?
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.
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
...
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.
Copy link to clipboard
Copied
Here you go, thank you !
https://drive.google.com/drive/folders/1ahF2A0KxPYzzkVku87e7npKy_r7VIHwx?usp=sharing
Copy link to clipboard
Copied
How did you create the code you originally posted?
Copy link to clipboard
Copied
How did you create the code you originally posted?
By @c.pfaffenbichler
@Nerosetsfire wrote: “I had chatgpt write a script"
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Where did you get the line
currentDoc.selection.selectSubject();
from?
Could it be a generative AI hallucination?
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.
Copy link to clipboard
Copied
still gave me the same error 😞
Copy link to clipboard
Copied
Seems to work here with the correction of the garbage-line of code @Stephen Marsh recommended.
Copy link to clipboard
Copied
Is this what the code should look like?
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
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?
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
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
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
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);
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.");
};
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!
Copy link to clipboard
Copied
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.