Skip to main content
Participant
February 24, 2023
Question

Mass replacing white (255) background to a new color using photoshop script

  • February 24, 2023
  • 1 reply
  • 661 views

Hello,

 

I want to batch replace white backgrounds (255,255,255) to a different solid color for some product images.

I found this script that almost does what I want, I just can figure out how to make it use the Select 'Color Range' in photoshop instead of the Select 'Object'.

 

What settings I need in the color range setting are;

 

Select: Highlights

Fuzziness:0%

Range:255 

 

Everything else in the script seems to be okay for what I need.

 

TIA

Here is the scrip that I am "using".

 

 

 

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 = 224

 

colorRef.rgb.green = 222

 

colorRef.rgb.blue = 216

 

 

 

// Now apply fill to the current selection

 

app.activeDocument.selection.fill(colorRef)

 

 

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

 

var fileName = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 

saveJPG (new File(saveFolder +'/'+ Date.now() + "_" + fileName + '.jpg'),12);

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

//fileList[a].remove();

 }

 

function saveJPG(saveFile, jpegQuality) {

 

    saveFile = (saveFile instanceof File) ? saveFile : new File(saveFile);

 

    jpegQuality = jpegQuality || 12;

 

    var jpgSaveOptions = new JPEGSaveOptions();

 

    jpgSaveOptions.embedColorProfile = true;

 

    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

 

    jpgSaveOptions.matte = MatteType.NONE;

 

    jpgSaveOptions.quality = jpegQuality;

 

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

 

}

 

This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
February 24, 2023

@Matt28578228m51k – Here is a function from the ScriptingListener plug-in, tidied up using Clean SL:

 

colorRangeHighlights(0, 255, false);

function colorRangeHighlights(highlightsFuzziness, highlightsLowerLimit, invert) {
    activeDocument.selection.deselect();
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	descriptor.putEnumerated( s2t( "colors" ), s2t( "colors" ), s2t( "highlights" ));
	descriptor.putInteger( s2t( "highlightsFuzziness" ), highlightsFuzziness );
	descriptor.putInteger( s2t( "highlightsLowerLimit" ), highlightsLowerLimit );
	descriptor.putBoolean( s2t( "invert" ), invert );
	descriptor.putInteger( s2t( "colorModel" ), 0 );
	executeAction( s2t( "colorRange" ), descriptor, DialogModes.NO );
}

 

https://helpx.adobe.com/au/photoshop/kb/downloadable-plugins-and-content.html

 

https://github.com/rendertom/Clean-SL

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/clean-sl/td-p/9358420