Skip to main content
Inspiring
January 16, 2022
Answered

Mass removing background using photoshop script

  • January 16, 2022
  • 6 replies
  • 8696 views

Hello there

Need help with this script:

 

 

var sourceFolder = Folder("C:\\ps\\src");

  if (sourceFolder != null)
  {
     var fileList = sourceFolder.getFiles();
	 //comment the above line and uncomment the following line to filter specific file types. the script will not work if you have any non-image file in the src folder so try filtering files types if the script fails.
	 // var fileList = sourceFolder..getFiles(/\.(jpg|tif|psd|crw|cr2|nef|dcr|dc2|raw|heic)$/i);
  }

for(var a = 0 ;a < fileList.length; a++){
    
app.open(fileList[a]);

// Select subject
var idautoCutout = stringIDToTypeID( "autoCutout" );
var desc01 = new ActionDescriptor();
var idsampleAllLayers = stringIDToTypeID( "sampleAllLayers" );
desc01.putBoolean( idsampleAllLayers, false );
try{
executeAction( idautoCutout, desc01, DialogModes.NO );
}
catch(err){}
// Invert the selection

app.activeDocument.selection.invert();

// Create a color to be used with the fill command

var colorRef = new SolidColor

colorRef.rgb.red = 0

colorRef.rgb.green = 0

colorRef.rgb.blue = 0



// Now apply fill to the current selection

app.activeDocument.selection.fill(colorRef)


//enter path for where you want the file saved
var saveFolder = new Folder("C:\\ps\\out"); 

var fileName = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
sfwPNG24 (new File(saveFolder +'/' + fileName + '.png'),12);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
//fileList[a].remove();
 }

function sfwPNG24(saveFile){

var pngOpts = new PNGSaveOptions;

pngOpts.transparency = true

pngOpts.compression = 9;

pngOpts.interlaced = false;

activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);

}

 

 

Right now script works and it saves files from folder to .png

But i need a transparent background for my images..

 

Need something that can delete selected pixels after this:

 

 

app.activeDocument.selection.invert();

 

 

 

In Photoshop there is button "Delete" (Edit -> Clear), when you press this it deletes pixels in selected area. I need this in script. How to do this?

 

 

 

Maybe i need somehow to simulate

This topic has been closed for replies.
Correct answer jawfoto

I am now running the action perfectly.

 

I deleted the make text in the actions, put a stop action at the start to make new text, wrote the text, started the action again, put a stop before make selection, made selection of new text (ctrl+click) and restarted the action, all the effects are applied.

 

Thanks all.

6 replies

jawfotoAuthorCorrect answer
Inspiring
January 18, 2022

I am now running the action perfectly.

 

I deleted the make text in the actions, put a stop action at the start to make new text, wrote the text, started the action again, put a stop before make selection, made selection of new text (ctrl+click) and restarted the action, all the effects are applied.

 

Thanks all.

New Participant
February 19, 2023

can you share the script text 

thank you

 

Bojan Živković11378569
Community Expert
January 17, 2022

From what I can see in tutorial (I am not watching tutorial just quick look at effect) it seems that more then only layer effects are used, there are multiple layers included in final effect. This will be difficult to replicate/record for novice action developer. 

From what I understand, you want to write more text and to replicate entire effect? It is doable, record action to create effect then type text on top of other layers and run action. Not sure why you must duplicate existing text then change it on duplicated layer?  Type it then run action or record text typing into action.

 

If you are trying to keep effect by duplicating type layer that won't work I think because it seems many separate raster layers are involved (just guessing by looking at Layers panel on screenshot above, I havent watched tutorial). It is not that simple and easy to copy effects when things are done in this way, you must record steps to replicate effect.

jawfotoAuthor
Inspiring
January 17, 2022

Thank you, this is helpful.

 

I will try to redesign the action, it seems like since the text layers are selections of the text converted to black and white layer mask, the action doesn't make new selections of the updated text recorded, i guess i have to make new selections of the new text also then.

 

When working with live text layers for some reason the filter gallery effects doesn't work so that's why selections are used instead of text layer (smart object).

 

When i convert text layer to smart object i can add effects/filters and just playback a action with whatever text, so the problem are the selection layers i guess.

 

Bojan Živković11378569
Community Expert
January 17, 2022

"i guess i have to make new selections of the new text also then."

For layer mask? Yes, it is not difficult if you have raster layer just Ctrl + click on layer thumb to load selection then create layer mask.

 

Be careful when recording action to not click on layer when selecting it to not record layer name in action step or name layers but that will not work if you plan to add more text and apply same effect. Best option is to use keyboard to select/target and move layers.

 

Do not hesitate to ask more questions and post action or take screenshots of steps and explain what are you trying to do and where is the problem. Do not expect easy things at least in the beginning until you learn how actions work and how to think when recording them.

Stephen Marsh
Community Expert
January 16, 2022

When you playback the action, does it successfully create all the effects?

jawfotoAuthor
Inspiring
January 16, 2022

Yes it does, perfectly. So it's just a matter of changing the text i can't figure out.

Stephen Marsh
Community Expert
January 16, 2022

Offhand I think that there are two choices.

 

1) Edit the action before playback: In the list view of the action (not button mode), double click on the make text layer action step and change the wording and press the commit tick button to apply the text. This will update the action step. Delete the text layer, it was a means to an end. Then run the action from the start and it will apply all of the steps, including the updated text.

 

2) Insert a stop step (without continue) after the make text layer step. One the action has created the text layer and the action has stopped, you can then edit the text added by the action. Once happy with the new text, you can then resume playback of the action from the point where it stopped and it will then play through to completion.

jawfotoAuthor
Inspiring
January 16, 2022

https://www.youtube.com/watch?v=hwRwUeVoFTo

 

Do this once, change the text, and auto add all of this on top, is it possible?

c.pfaffenbichler
Community Expert
January 16, 2022

A Smart Object approach might considerably improve the process, but as @Stephen Marsh already mentioned the description does not seem sufficient to provide meaningful advice. 

Stephen Marsh
Community Expert
January 16, 2022

Can you provide screenshot of the layers panel or perhaps a link to the layered PSD file?

 

As a general comment without seeing the file, when it comes to text, InDesign is often the go to app. If the text requires a lot of embellishing and you have access to Adobe Illustrator, the Appearance panel and various live effects are very useful. Photoshop would generally be my second choice after Illustrator for such things, however it all depends on what is going on.