• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

script of percentage of active layer from the entire canvas

Engaged ,
Jan 26, 2019 Jan 26, 2019

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 27, 2019 Jan 27, 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 = ne

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 26, 2019 Jan 26, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 27, 2019 Jan 27, 2019

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 27, 2019 Jan 27, 2019

Copy link to clipboard

Copied

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 );

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 28, 2019 Jan 28, 2019

Copy link to clipboard

Copied

Works!

easy and did the job, thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 28, 2019 Jan 28, 2019

Copy link to clipboard

Copied

Glad it worked.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 10, 2019 Mar 10, 2019

Copy link to clipboard

Copied

Sorry to bring this thread out of the dead, just wanted to ask a question about the script.

I've noticed that perc returns 1 in cases where I have an empty layer and I have a full layer, shouldn't an empty layer give 0? how do we differ an empty layer from a full layer?

picture examples:

EMPTY LAYER (covers 0% of the canvas)

FULL LAYER (covers 100% of the canvas)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 10, 2019 Mar 10, 2019

Copy link to clipboard

Copied

I got a solution myself here   I've used r-bin's copy() function that checks if a copy can be made using try and catch from this thread Try to copy when no pixels where selected

#target photoshop 

   

    var perc = getSelc(); 

    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 );  

   

    var his = app.activeDocument.histogram 

    var hisCount = 0 

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

    hisCount+= his 

    } 

   

    app.activeDocument.selection.selectAll();

    if (hisCount == app.activeDocument.width.value*app.activeDocument.height.value)                        // perc = 1 in two cases. layer is empty, layer is full

    {

        if (copy())                                                                                        // if it can copy it means perc isn't empty

        {

            return 1;

        }

        else

        {

            return 0;

        }

    }

   

    return hisCount/(app.activeDocument.width.value*app.activeDocument.height.value);

function copy() 

{

        try

        {

            executeAction(stringIDToTypeID("copyEvent"), undefined, DialogModes.NO);

        }

        catch(e)

        {

            return false;

        } 

        return true; 

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2019 Mar 10, 2019

Copy link to clipboard

Copied

LATEST

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 );

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines