Skip to main content
alexeys49200182
Participant
January 5, 2022
Answered

Mass removing background using photoshop script

  • January 5, 2022
  • 3 replies
  • 1089 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 Stephen Marsh

You can use code such as:

 

 

app.activeDocument.selection.clear();

 

 

Or:

 

 

var iddelete = stringIDToTypeID( "delete" );
executeAction( iddelete, undefined, DialogModes.NO );

 

 

If this was outside of scripting and batch automation, I would advise to use a layer mask instead.

 

 

maskSelection("revealSelection");

function maskSelection(maskParameter) {
    // Parameter = "revealSelection" or "hideSelection"
    var idmake = stringIDToTypeID("make");
    var desc500 = new ActionDescriptor();
    var idnew = stringIDToTypeID("new");
    var idchannel = stringIDToTypeID("channel");
    desc500.putClass(idnew, idchannel);
    var idat = stringIDToTypeID("at");
    var ref22 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idchannel = stringIDToTypeID("channel");
    var idmask = stringIDToTypeID("mask");
    ref22.putEnumerated(idchannel, idchannel, idmask);
    desc500.putReference(idat, ref22);
    var idusing = stringIDToTypeID("using");
    var iduserMaskEnabled = stringIDToTypeID("userMaskEnabled");
    var idrevealSelection = stringIDToTypeID(maskParameter);
    desc500.putEnumerated(idusing, iduserMaskEnabled, idrevealSelection);
    executeAction(idmake, desc500, DialogModes.NO);
}

 

 

Or:

 

maskSelection("revealSelection");

function maskSelection(maskParameter) {
    // Parameter = "revealSelection" or "hideSelection"
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	descriptor.putClass( s2t( "new" ), s2t( "channel" ));
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
	descriptor.putReference( s2t( "at" ), reference );
	descriptor.putEnumerated( s2t( "using" ), s2t( "userMaskEnabled" ), s2t( maskParameter ));
	executeAction( s2t( "make" ), descriptor, DialogModes.NO );
}

 

 

P.S. I don't see any conditional logic to check for a layer vs. a flattened background layer... Can you safely assume that all input files will be a layer and not a Background image?

 

3 replies

Participant
January 17, 2022

can you please tell if i want clear background (no solid background or layer with subject) ??

Stephen Marsh
Community Expert
Community Expert
January 17, 2022

I'm not understanding your post, sorry, I think more information, screenshots etc may help.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 5, 2022

You can use code such as:

 

 

app.activeDocument.selection.clear();

 

 

Or:

 

 

var iddelete = stringIDToTypeID( "delete" );
executeAction( iddelete, undefined, DialogModes.NO );

 

 

If this was outside of scripting and batch automation, I would advise to use a layer mask instead.

 

 

maskSelection("revealSelection");

function maskSelection(maskParameter) {
    // Parameter = "revealSelection" or "hideSelection"
    var idmake = stringIDToTypeID("make");
    var desc500 = new ActionDescriptor();
    var idnew = stringIDToTypeID("new");
    var idchannel = stringIDToTypeID("channel");
    desc500.putClass(idnew, idchannel);
    var idat = stringIDToTypeID("at");
    var ref22 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idchannel = stringIDToTypeID("channel");
    var idmask = stringIDToTypeID("mask");
    ref22.putEnumerated(idchannel, idchannel, idmask);
    desc500.putReference(idat, ref22);
    var idusing = stringIDToTypeID("using");
    var iduserMaskEnabled = stringIDToTypeID("userMaskEnabled");
    var idrevealSelection = stringIDToTypeID(maskParameter);
    desc500.putEnumerated(idusing, iduserMaskEnabled, idrevealSelection);
    executeAction(idmake, desc500, DialogModes.NO);
}

 

 

Or:

 

maskSelection("revealSelection");

function maskSelection(maskParameter) {
    // Parameter = "revealSelection" or "hideSelection"
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	descriptor.putClass( s2t( "new" ), s2t( "channel" ));
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
	descriptor.putReference( s2t( "at" ), reference );
	descriptor.putEnumerated( s2t( "using" ), s2t( "userMaskEnabled" ), s2t( maskParameter ));
	executeAction( s2t( "make" ), descriptor, DialogModes.NO );
}

 

 

P.S. I don't see any conditional logic to check for a layer vs. a flattened background layer... Can you safely assume that all input files will be a layer and not a Background image?

 

try67
Community Expert
Community Expert
January 5, 2022

[Question moved to the PS forum from the Acrobat one]