Skip to main content
Participant
June 22, 2018
Answered

How to check if a layer contains any transparent pixels?

  • June 22, 2018
  • 2 replies
  • 2928 views

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.

This topic has been closed for replies.
Correct answer nickcombs

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

 


Thanks for catching that @Stephen 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 )}

 

2 replies

Legend
June 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("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); }

    }

nickcombs
Inspiring
March 18, 2022

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.

nickcombs
nickcombsCorrect answer
Inspiring
March 21, 2022

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

 


Thanks for catching that @Stephen 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 )}

 

Tom Winkelmann
Inspiring
June 22, 2018

You can take this thread as start...

 

checking each individual pixel of a layer for transparency