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

How to check if a layer contains any transparent pixels?

New Here ,
Jun 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

I want to export layers to image files(png, jpg). To export smaller image files, I want to export layers with transparent pixels to PNG file, and layers without any transparent pixels to JPG files. I think I need to get the color of every pixel of the layer, then check if any of the color contains transparency. So how can I do this with
ExtendScript? I checked the API reference and google a lot, but didn't find a solution.

TOPICS
Actions and scripting

Views

2.3K

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 2 Correct answers

People's Champ , Jun 22, 2018 Jun 22, 2018

alert("Layer " + (layer_is_solid()?"does not have ":"has a ") + "transparency")

function layer_is_solid()

    {

    try {

        var solid = false;

        selection_from_layer();

        try { app.activeDocument.selection.feather(0); } catch(e) { solid = true; }

        app.activeDocument.selection.deselect();

        return solid;

        }

    catch(e) { alert(e); throw(e); }

    }

function selection_from_layer()

    {

    try {

        var r1 = new ActionReference();

        r1.putProperty(charIDToTypeID(

...

Votes

Translate

Translate
Explorer , Mar 21, 2022 Mar 21, 2022

Thanks for catching that @Stephen_A_Marsh. I have updated the code below with a layer_is_empty function to handle that special case.

// https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-if-a-layer-contains-any-transparent-pixels/td-p/9917339



alert( "Layer is " +( layer_is_solid() ? "solid" : "transparent" ) )



function layer_is_solid() {
  try {
    displayDialogs = DialogModes.NO
    var solid = false
    var doc = app.activeDocument
    selection_from_layer()
    
...

Votes

Translate

Translate
Adobe
Advocate ,
Jun 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

You can take this thread as start...

 

checking each individual pixel of a layer for transparency

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
People's Champ ,
Jun 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

alert("Layer " + (layer_is_solid()?"does not have ":"has a ") + "transparency")

function layer_is_solid()

    {

    try {

        var solid = false;

        selection_from_layer();

        try { app.activeDocument.selection.feather(0); } catch(e) { solid = true; }

        app.activeDocument.selection.deselect();

        return solid;

        }

    catch(e) { alert(e); throw(e); }

    }

function selection_from_layer()

    {

    try {

        var r1 = new ActionReference();

        r1.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));

        var d = new ActionDescriptor();

        d.putReference(charIDToTypeID("null"), r1);

        var r2 = new ActionReference();

        r2.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Trsp"));

        d.putReference(charIDToTypeID("T   "), r2);

        executeAction(charIDToTypeID("setd"), d, DialogModes.NO);

        }

    catch(e) { throw(e); }

    }

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
Explorer ,
Mar 18, 2022 Mar 18, 2022

Copy link to clipboard

Copied

Interesting workaround. For me, it gives false on an empty layer and true on layers with any non-transparent pixels. But I'm looking for a way to flag any layer that isn't completely solid.

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
Explorer ,
Mar 18, 2022 Mar 18, 2022

Copy link to clipboard

Copied

Here is the modification that works for me. I added the invert_selection_from_layer function and some displayDialogs managment. 

// https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-if-a-layer-contains-any-transparent-pixels/td-p/9917339



alert("Layer " + (layer_is_solid() ? "does not have " : "has a ") + "transparency")



function layer_is_solid()
{
  try {
    displayDialogs = DialogModes.NO
    var solid = false;
    selection_from_layer();
    invert_selection_from_layer();
    try {
      app.activeDocument.selection.feather(0);
    } catch (e) {
      solid = true;
    }
    app.activeDocument.selection.deselect();
    displayDialogs = DialogModes.ERROR
    return solid;
  } catch (e) {
    displayDialogs = DialogModes.ERROR
    alert(e);
    throw (e);
  }
} // layer_is_solid



function selection_from_layer()
{
  try {
    var r1 = new ActionReference();
    r1.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));
    var d = new ActionDescriptor();
    d.putReference(charIDToTypeID("null"), r1);
    var r2 = new ActionReference();
    r2.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Trsp"));
    d.putReference(charIDToTypeID("T   "), r2);
    executeAction(charIDToTypeID("setd"), d, DialogModes.NO);
  } catch (e) {
    throw (e);
  }
} // selection_from_layer


function invert_selection_from_layer() {
  try {
    executeAction( charIDToTypeID( "Invs" ), undefined, DialogModes.NO );
  } catch (e) {
    throw (e);
  }
} // invert_selection_from_layer

 

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 18, 2022 Mar 18, 2022

Copy link to clipboard

Copied

@nickcombs thanks for contributing... For me, your code is still flagging a single transparent layer with no content as not having transparency.

 

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
Explorer ,
Mar 21, 2022 Mar 21, 2022

Copy link to clipboard

Copied

LATEST

Thanks for catching that @Stephen_A_Marsh. I have updated the code below with a layer_is_empty function to handle that special case.

// https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-if-a-layer-contains-any-transparent-pixels/td-p/9917339



alert( "Layer is " +( layer_is_solid() ? "solid" : "transparent" ) )



function layer_is_solid() {
  try {
    displayDialogs = DialogModes.NO
    var solid = false
    var doc = app.activeDocument
    selection_from_layer()
    invert_selection_from_layer()
    try { doc.selection.feather(0) }
    catch (e) { solid = true }
    if( layer_is_empty( doc.activeLayer) ) solid = false
    doc.selection.deselect()
    displayDialogs = DialogModes.ERROR
    return solid
  } catch (e) {
    displayDialogs = DialogModes.ERROR
    throw ( 'layer_is_solid >> '+ e )
  }
} // layer_is_solid



function selection_from_layer() {
  try {
    var r1 = new ActionReference()
    r1.putProperty( chID("Chnl"), chID("fsel") )
    var d = new ActionDescriptor()
    d.putReference( chID("null"), r1 )
    var r2 = new ActionReference()
    r2.putEnumerated( chID("Chnl"), chID("Chnl"), chID("Trsp") )
    d.putReference( chID("T   "), r2 )
    executeAction( chID("setd"), d, DialogModes.NO )
  } catch (e) {
    throw ( 'selection_from_layer >> '+ e )
  }
} // selection_from_layer



function invert_selection_from_layer() {
  try {
    executeAction( chID( "Invs" ), undefined, DialogModes.NO )
  } catch (e) {
    throw ( 'invert_selection_from_layer >> '+ e )
  }
} // invert_selection_from_layer



function layer_is_empty( artLayer ) {
  try {
    var bds = artLayer.bounds
    if( bds[0].value || bds[1].value || bds[2].value || bds[3].value )
      return false
    return true
  } catch (e) {
    throw ( 'layer_is_empty >> '+ e )
  }
} // layer_is_empty



function chID( a ) { return charIDToTypeID( a )}

 

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