Skip to main content
AG_Ps_100
Inspiring
January 27, 2019
Answered

script of percentage of active layer from the entire canvas

  • January 27, 2019
  • 2 replies
  • 1878 views

Hello everyone,

I have my active layer and it has some transparency in it and pixels that were already deleted.

I want to know what is the percentage of active layer from the entire canvas.

I also want it in a script because I want to use it afterwards in an IF conditional

thank you!

This topic has been closed for replies.
Correct answer Chuck Uebele

I forgot that a histogram will only get the pixels of a selection also. So this should work, as long as it's on a layer that doesn't have a layer mask.

#target photoshop

var doc = activeDocument

var curLay = doc.activeLayer

getSelc ();

var his = doc.histogram

var hisCount = 0

for (var i=0;i<his.length;i++){

    hisCount+= his

    }

var perc= hisCount/(doc.width.value*doc.height.value)

doc.selection.deselect()

alert (perc);

function getSelc(){

       var idsetd = charIDToTypeID( "setd" );

        var desc7 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref5 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idfsel = charIDToTypeID( "fsel" );

            ref5.putProperty( idChnl, idfsel );

        desc7.putReference( idnull, ref5 );

        var idT = charIDToTypeID( "T   " );

            var ref6 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idChnl = charIDToTypeID( "Chnl" );

            var idTrsp = charIDToTypeID( "Trsp" );

            ref6.putEnumerated( idChnl, idChnl, idTrsp );

        desc7.putReference( idT, ref6 );

    executeAction( idsetd, desc7, DialogModes.NO );

    }

2 replies

Chuck Uebele
Community Expert
Community Expert
March 11, 2019

My script uses the histogram, and it views transparency as white, so a layer that is all transparent will register as 100% or 1 in the alert. I modified the script to check for transparency.

#target photoshop

var doc = activeDocument

var curLay = doc.activeLayer

var trans = true;

getSelc ();

try{var b = doc.selection.bounds}

catch(e){trans = false}

var his = doc.histogram

var hisCount = 0

for (var i=0;i<his.length;i++){

    hisCount+= his

    }

var perc= hisCount/(doc.width.value*doc.height.value)

doc.selection.deselect()

if(trans){alert (perc)}

else{alert('The layer is transparent.')}

function getSelc(){

       var idsetd = charIDToTypeID( "setd" );

        var desc7 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref5 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idfsel = charIDToTypeID( "fsel" );

            ref5.putProperty( idChnl, idfsel );

        desc7.putReference( idnull, ref5 );

        var idT = charIDToTypeID( "T   " );

            var ref6 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idChnl = charIDToTypeID( "Chnl" );

            var idTrsp = charIDToTypeID( "Trsp" );

            ref6.putEnumerated( idChnl, idChnl, idTrsp );

        desc7.putReference( idT, ref6 );

    executeAction( idsetd, desc7, DialogModes.NO );

    }

Chuck Uebele
Community Expert
Community Expert
January 27, 2019

I can think of two possible ways to do this.

One way would be to use the histogram. Unfortunately it registered transparent as white, so a work around would be to get the histogram reading of 255 then temporarily create a black layer below everything and read the histogram again at 255 and compare the difference. That should be the number of pixels that are transparent. Then just use that number by the total number of pixels to get your percentage.

The other way, which I'm not sure about doing would be to make a selection of the visible pixels by ctrl/cmd clicking on the layer thumbnail. You might have to make a merged stamp visible layer to do this if multiple layers are involved. The. You can you analysis to record the area. You them might be able to use AM code to get the area and again compare it to the total  number of pixels. If the area can't be e yea Ted using AM code, you could use scriptlistener export the data to a text or CVS file that you could then read and make you calculations.

AG_Ps_100
AG_Ps_100Author
Inspiring
January 27, 2019

I saw in histogram I can get pixels of selected layer, I just need this info in a script (if it's shown in photoshop it's definitely possible)

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
January 28, 2019

I forgot that a histogram will only get the pixels of a selection also. So this should work, as long as it's on a layer that doesn't have a layer mask.

#target photoshop

var doc = activeDocument

var curLay = doc.activeLayer

getSelc ();

var his = doc.histogram

var hisCount = 0

for (var i=0;i<his.length;i++){

    hisCount+= his

    }

var perc= hisCount/(doc.width.value*doc.height.value)

doc.selection.deselect()

alert (perc);

function getSelc(){

       var idsetd = charIDToTypeID( "setd" );

        var desc7 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref5 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idfsel = charIDToTypeID( "fsel" );

            ref5.putProperty( idChnl, idfsel );

        desc7.putReference( idnull, ref5 );

        var idT = charIDToTypeID( "T   " );

            var ref6 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idChnl = charIDToTypeID( "Chnl" );

            var idTrsp = charIDToTypeID( "Trsp" );

            ref6.putEnumerated( idChnl, idChnl, idTrsp );

        desc7.putReference( idT, ref6 );

    executeAction( idsetd, desc7, DialogModes.NO );

    }