Need Help To Modify This Script

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

jmarkt

Need Help To Modify This Script

Post by jmarkt »

The script below modifies PNG format clipart by filling current transparent areas within the clipart to black, which is desired. The black lines are also converted to white, which I would like to modify so that that white would be "knocked out", or made transparent. Here is a link to reflect current and desired: https://jumpshare.com/b/Rxbfz8Xr42NUiBOwEuTS

Code: Select all  if (app.documents.length > 0) {
    //////////////////////////////////////////
    var myDocument = app.activeDocument;
    // check there is but one layer;
    var originalUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    myDocument.flatten();
    var theLayer = myDocument.activeLayer;
    // change canvas to ascertain white background edge;
    resetForeAndBackgroud ();
    var theW = myDocument.width;
    var theH = myDocument.height;
    myDocument.resizeCanvas(theW + 4, theH + 4, AnchorPosition.MIDDLECENTER);
    // load red channel;
    myDocument.selection.load(myDocument.channels[0], SelectionType.REPLACE, true);
    // create layer;
    var whiteLayer = solidColorLayer (255,255,255);
    // check upper left pixel is white;
    myDocument.activeLayer = theLayer;
    var theX = 1;
    var theY = 1;
    magicWand(theX, theY);
    // expand and invert selection;
    myDocument.selection.expand(1);
    myDocument.selection.invert();
    // create layer;
    var blackLayer = solidColorLayer (0,0,0);
    // remove original layer and merge;
    theLayer.remove();
    if (myDocument.layers.length > 1) {myDocument.mergeVisibleLayers()};
    // reset;
    myDocument.resizeCanvas(theW, theH, AnchorPosition.MIDDLECENTER);
    app.preferences.rulerUnits = originalUnits;
    };
    //////////////////////////////////////////
    ////// function to create a solid color layer //////
    function solidColorLayer (theRed, theGreen, theBlue) {
    // =======================================================
    var idMk = charIDToTypeID( "Mk  " );
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            ref1.putClass( idcontentLayer );
        desc3.putReference( idnull, ref1 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc4 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
                var desc5 = new ActionDescriptor();
                var idClr = charIDToTypeID( "Clr " );
                    var desc6 = new ActionDescriptor();
                    var idRd = charIDToTypeID( "Rd  " );
                    desc6.putDouble( idRd, theRed );
                    var idGrn = charIDToTypeID( "Grn " );
                    desc6.putDouble( idGrn,theGreen );
                    var idBl = charIDToTypeID( "Bl  " );
                    desc6.putDouble( idBl, theBlue );
                var idRGBC = charIDToTypeID( "RGBC" );
                desc5.putObject( idClr, idRGBC, desc6 );
            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
            desc4.putObject( idType, idsolidColorLayer, desc5 );
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        desc3.putObject( idUsng, idcontentLayer, desc4 );
    executeAction( idMk, desc3, DialogModes.NO );
    return app.activeDocument.activeLayer
    };
    ////// magic wand //////
    function magicWand (theX, theY) {
    //use magic wand
        var idsetd = charIDToTypeID( "setd" );
            var desc263 = new ActionDescriptor();
            var idnull = charIDToTypeID( "null" );
                var ref123 = new ActionReference();
                var idChnl = charIDToTypeID( "Chnl" );
                var idfsel = charIDToTypeID( "fsel" );
                ref123.putProperty( idChnl, idfsel );
            desc263.putReference( idnull, ref123 );
            var idT = charIDToTypeID( "T   " );
                var desc264 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc264.putUnitDouble( idHrzn, idRlt, theX);
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc264.putUnitDouble( idVrtc, idRlt, theY);
            var idPnt = charIDToTypeID( "Pnt " );
            desc263.putObject( idT, idPnt, desc264 );
            var idTlrn = charIDToTypeID( "Tlrn" );
            desc263.putInteger( idTlrn, 1 );
        executeAction( idsetd, desc263, DialogModes.NO );
    };
    ////// get color //////
    function getColor (theX, theY) {
    // select color sampler tool;
    // =======================================================
    var idslct = charIDToTypeID( "slct" );
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref2 = new ActionReference();
            var idcolorSamplerTool = stringIDToTypeID( "colorSamplerTool" );
            ref2.putClass( idcolorSamplerTool );
        desc3.putReference( idnull, ref2 );
        var iddontRecord = stringIDToTypeID( "dontRecord" );
        desc3.putBoolean( iddontRecord, true );
        var idforceNotify = stringIDToTypeID( "forceNotify" );
        desc3.putBoolean( idforceNotify, true );
    executeAction( idslct, desc3, DialogModes.NO );
    // set sampler;
    // =======================================================
    var idMk = charIDToTypeID( "Mk  " );
        var desc4 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref3 = new ActionReference();
            var idClSm = charIDToTypeID( "ClSm" );
            ref3.putClass( idClSm );
        desc4.putReference( idnull, ref3 );
        var idPstn = charIDToTypeID( "Pstn" );
            var desc5 = new ActionDescriptor();
            var idHrzn = charIDToTypeID( "Hrzn" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc5.putUnitDouble( idHrzn, idPxl, theX );
            var idVrtc = charIDToTypeID( "Vrtc" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc5.putUnitDouble( idVrtc, idPxl, theY );
        var idPnt = charIDToTypeID( "Pnt " );
        desc4.putObject( idPstn, idPnt, desc5 );
    executeAction( idMk, desc4, DialogModes.NO );
    // forground color;
    var theSampler = activeDocument.colorSamplers[activeDocument.colorSamplers.length - 1];
    var theColor = theSampler.color;
    theSampler.remove();
    app.preferences.rulerUnits = originalUnits;
    return [theColor.rgb.red, theColor.rgb.green, theColor.rgb.blue];
    };
    ////// reset fore- and background color //////
    function resetForeAndBackgroud () {
    // =======================================================
    var idRset = charIDToTypeID( "Rset" );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idClr = charIDToTypeID( "Clr " );
            var idClrs = charIDToTypeID( "Clrs" );
            ref1.putProperty( idClr, idClrs );
        desc2.putReference( idnull, ref1 );
    executeAction( idRset, desc2, DialogModes.NO );
    };
    ////// is black and white //////
    function isBlackAndWhite () {
    // =======================================================
    var idDplc = charIDToTypeID( "Dplc" );
        var desc5 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref4 = new ActionReference();
            var idDcmn = charIDToTypeID( "Dcmn" );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idFrst = charIDToTypeID( "Frst" );
            ref4.putEnumerated( idDcmn, idOrdn, idFrst );
        desc5.putReference( idnull, ref4 );
        var idMrgd = charIDToTypeID( "Mrgd" );
        desc5.putBoolean( idMrgd, true );
    executeAction( idDplc, desc5, DialogModes.NO );
    // =======================================================
    var idCnvM = charIDToTypeID( "CnvM" );
        var desc6 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
        var idLbCM = charIDToTypeID( "LbCM" );
        desc6.putClass( idT, idLbCM );
    executeAction( idCnvM, desc6, DialogModes.NO );
    // the temp file;
    var theFile = app.activeDocument;
    // the histograms of a and b channels;
    var histo1 = theFile.channels[1].histogram;
    var histo2 = theFile.channels[1].histogram;
    // the total pixels;
    var theNumber = theFile.width * theFile.height;
    // close temp file;
    theFile.close(SaveOptions.DONOTSAVECHANGES);
    // check;
    if (histo1[127]+histo1[128]+histo1[129] == theNumber && histo2[127]+histo2[128]+histo2[129] == theNumber) {return true}
    else {return false}
    };
pfaffenbichler

Need Help To Modify This Script

Post by pfaffenbichler »

I think you should restructure the process.
First create the Selection that you currently use for the black Layer, then load the Red Channel, but instead of SelectionType.REPLACE use DIMINISH and create only one Layer.
pfaffenbichler

Need Help To Modify This Script

Post by pfaffenbichler »

Is this clearer?
Code: Select allif (app.documents.length > 0) {
    //////////////////////////////////////////
    var myDocument = app.activeDocument;
    // check there is but one layer;
    var originalUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    myDocument.flatten();
    var theLayer = myDocument.activeLayer;
    // change canvas to ascertain white background edge;
    resetForeAndBackgroud ();
    var theW = myDocument.width;
    var theH = myDocument.height;
    myDocument.resizeCanvas(theW + 4, theH + 4, AnchorPosition.MIDDLECENTER);
    // load red channel;
//    myDocument.selection.load(myDocument.channels[0], SelectionType.REPLACE, true);
    // create layer;
//    var whiteLayer = solidColorLayer (255,255,255);
    // check upper left pixel is white;
    myDocument.activeLayer = theLayer;
    var theX = 1;
    var theY = 1;
    magicWand(theX, theY);
    // expand and invert selection;
    myDocument.selection.expand(1);
    myDocument.selection.invert();
    myDocument.selection.load(myDocument.channels[0], SelectionType.DIMINISH, true);
    // create layer;
    var blackLayer = solidColorLayer (0,0,0);
    // remove original layer and merge;
    theLayer.remove();
    if (myDocument.layers.length > 1) {myDocument.mergeVisibleLayers()};
    // reset;
    myDocument.resizeCanvas(theW, theH, AnchorPosition.MIDDLECENTER);
    app.preferences.rulerUnits = originalUnits;
    };
    //////////////////////////////////////////
    ////// function to create a solid color layer //////
    function solidColorLayer (theRed, theGreen, theBlue) {
    // =======================================================
    var idMk = charIDToTypeID( "Mk  " );
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            ref1.putClass( idcontentLayer );
        desc3.putReference( idnull, ref1 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc4 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
                var desc5 = new ActionDescriptor();
                var idClr = charIDToTypeID( "Clr " );
                    var desc6 = new ActionDescriptor();
                    var idRd = charIDToTypeID( "Rd  " );
                    desc6.putDouble( idRd, theRed );
                    var idGrn = charIDToTypeID( "Grn " );
                    desc6.putDouble( idGrn,theGreen );
                    var idBl = charIDToTypeID( "Bl  " );
                    desc6.putDouble( idBl, theBlue );
                var idRGBC = charIDToTypeID( "RGBC" );
                desc5.putObject( idClr, idRGBC, desc6 );
            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
            desc4.putObject( idType, idsolidColorLayer, desc5 );
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        desc3.putObject( idUsng, idcontentLayer, desc4 );
    executeAction( idMk, desc3, DialogModes.NO );
    return app.activeDocument.activeLayer
    };
    ////// magic wand //////
    function magicWand (theX, theY) {
    //use magic wand
        var idsetd = charIDToTypeID( "setd" );
            var desc263 = new ActionDescriptor();
            var idnull = charIDToTypeID( "null" );
                var ref123 = new ActionReference();
                var idChnl = charIDToTypeID( "Chnl" );
                var idfsel = charIDToTypeID( "fsel" );
                ref123.putProperty( idChnl, idfsel );
            desc263.putReference( idnull, ref123 );
            var idT = charIDToTypeID( "T   " );
                var desc264 = new ActionDescriptor();
                var idHrzn = charIDToTypeID( "Hrzn" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc264.putUnitDouble( idHrzn, idRlt, theX);
                var idVrtc = charIDToTypeID( "Vrtc" );
                var idRlt = charIDToTypeID( "#Rlt" );
                desc264.putUnitDouble( idVrtc, idRlt, theY);
            var idPnt = charIDToTypeID( "Pnt " );
            desc263.putObject( idT, idPnt, desc264 );
            var idTlrn = charIDToTypeID( "Tlrn" );
            desc263.putInteger( idTlrn, 1 );
        executeAction( idsetd, desc263, DialogModes.NO );
    };
    ////// get color //////
    function getColor (theX, theY) {
    // select color sampler tool;
    // =======================================================
    var idslct = charIDToTypeID( "slct" );
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref2 = new ActionReference();
            var idcolorSamplerTool = stringIDToTypeID( "colorSamplerTool" );
            ref2.putClass( idcolorSamplerTool );
        desc3.putReference( idnull, ref2 );
        var iddontRecord = stringIDToTypeID( "dontRecord" );
        desc3.putBoolean( iddontRecord, true );
        var idforceNotify = stringIDToTypeID( "forceNotify" );
        desc3.putBoolean( idforceNotify, true );
    executeAction( idslct, desc3, DialogModes.NO );
    // set sampler;
    // =======================================================
    var idMk = charIDToTypeID( "Mk  " );
        var desc4 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref3 = new ActionReference();
            var idClSm = charIDToTypeID( "ClSm" );
            ref3.putClass( idClSm );
        desc4.putReference( idnull, ref3 );
        var idPstn = charIDToTypeID( "Pstn" );
            var desc5 = new ActionDescriptor();
            var idHrzn = charIDToTypeID( "Hrzn" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc5.putUnitDouble( idHrzn, idPxl, theX );
            var idVrtc = charIDToTypeID( "Vrtc" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc5.putUnitDouble( idVrtc, idPxl, theY );
        var idPnt = charIDToTypeID( "Pnt " );
        desc4.putObject( idPstn, idPnt, desc5 );
    executeAction( idMk, desc4, DialogModes.NO );
    // forground color;
    var theSampler = activeDocument.colorSamplers[activeDocument.colorSamplers.length - 1];
    var theColor = theSampler.color;
    theSampler.remove();
    app.preferences.rulerUnits = originalUnits;
    return [theColor.rgb.red, theColor.rgb.green, theColor.rgb.blue];
    };
    ////// reset fore- and background color //////
    function resetForeAndBackgroud () {
    // =======================================================
    var idRset = charIDToTypeID( "Rset" );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idClr = charIDToTypeID( "Clr " );
            var idClrs = charIDToTypeID( "Clrs" );
            ref1.putProperty( idClr, idClrs );
        desc2.putReference( idnull, ref1 );
    executeAction( idRset, desc2, DialogModes.NO );
    };
    ////// is black and white //////
    function isBlackAndWhite () {
    // =======================================================
    var idDplc = charIDToTypeID( "Dplc" );
        var desc5 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref4 = new ActionReference();
            var idDcmn = charIDToTypeID( "Dcmn" );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idFrst = charIDToTypeID( "Frst" );
            ref4.putEnumerated( idDcmn, idOrdn, idFrst );
        desc5.putReference( idnull, ref4 );
        var idMrgd = charIDToTypeID( "Mrgd" );
        desc5.putBoolean( idMrgd, true );
    executeAction( idDplc, desc5, DialogModes.NO );
    // =======================================================
    var idCnvM = charIDToTypeID( "CnvM" );
        var desc6 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
        var idLbCM = charIDToTypeID( "LbCM" );
        desc6.putClass( idT, idLbCM );
    executeAction( idCnvM, desc6, DialogModes.NO );
    // the temp file;
    var theFile = app.activeDocument;
    // the histograms of a and b channels;
    var histo1 = theFile.channels[1].histogram;
    var histo2 = theFile.channels[1].histogram;
    // the total pixels;
    var theNumber = theFile.width * theFile.height;
    // close temp file;
    theFile.close(SaveOptions.DONOTSAVECHANGES);
    // check;
    if (histo1[127]+histo1[128]+histo1[129] == theNumber && histo2[127]+histo2[128]+histo2[129] == theNumber) {return true}
    else {return false}
    };
jmarkt

Need Help To Modify This Script

Post by jmarkt »

It absolutely did, pfaffenbichler! Helped my learning curve as well. Thanks again!