Skip to main content
zlatkoz26887948
Participant
May 3, 2020
Answered

Convert all alpha channels into layers.

  • May 3, 2020
  • 3 replies
  • 4528 views

Hi everyone. I really need your help. I'm trying to find a script that can convert all alpha channels into different layers and if one or more alpha channels is/are empty/black to be deleted.

Thank you

This topic has been closed for replies.
Correct answer c.pfaffenbichler

It’s best to use the “Apply Image” command.

 
You can also duplicate a channel into a new document, convert it to Gray mode and duplicate this layer back.
 

You are right, Apply Image might also be faster. 

// create layers for channels that are not empty in rgb images;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
if (activeDocument.mode == DocumentMode.RGB) {
var theChannels = collectChannels ();
activeDocument.selection.deselect();
// create layers;
for (var m = theChannels.length-1; m >= 0; m--) {
loadAndLayer (theChannels[m][1], theChannels[m][0], [0,0,0]);
};
};
};
////// collect layers //////
function collectChannels () {
// get number of layers;
var ref = new ActionReference();
//ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfChannels'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfChannels"));
// pixel dimensions;
var theWidth = applicationDesc.getUnitDoubleValue(stringIDToTypeID("width"));
var theHeight = applicationDesc.getUnitDoubleValue(stringIDToTypeID("height"));
var theRes = applicationDesc.getUnitDoubleValue(stringIDToTypeID("resolution"));
var thePixels = (theWidth*theRes/72)*(theHeight*theRes/72);
// process the channels;
var theChannels = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex(stringIDToTypeID( "channel" ), m);
var channelDesc = executeActionGet(ref);
// collect values;
var theName = channelDesc.getString(stringIDToTypeID('channelName'));
var theIndex = channelDesc.getInteger(stringIDToTypeID('itemIndex'));
var theID = channelDesc.getInteger(stringIDToTypeID('ID'));
if (channelDesc.hasKey(stringIDToTypeID('alphaChannelOptions')) == true) {
var theHistogram = channelDesc.getList(stringIDToTypeID('histogram'));
var value0 = theHistogram.getInteger(0);
// if not completely black;
if (value0 != thePixels) {
theChannels.push([theName, theIndex, theID])
} else {}
};
} catch (e) {};
};
return theChannels
};
//////
function loadAndLayer (theIndex, theName, theColor) {
var idnull = stringIDToTypeID( "null" );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idchannel = stringIDToTypeID( "channel" );
var idmake = stringIDToTypeID( "make" );
// =======================================================
var desc11 = new ActionDescriptor();
var ref4 = new ActionReference();
ref4.putClass( idcontentLayer );
desc11.putReference( idnull, ref4 );
var desc12 = new ActionDescriptor();
    var desc13 = new ActionDescriptor();
        var desc14 = new ActionDescriptor();
        desc14.putDouble( stringIDToTypeID( "red" ), theColor[0] );
        desc14.putDouble( stringIDToTypeID( "grain" ), theColor[1] );
        desc14.putDouble( stringIDToTypeID( "blue" ), theColor[2] );
    var idRGBColor = stringIDToTypeID( "RGBColor" );
    desc13.putObject( stringIDToTypeID( "color" ), idRGBColor, desc14 );
desc12.putString( stringIDToTypeID( "name" ), theName );
desc12.putObject( stringIDToTypeID( "type" ), stringIDToTypeID( "solidColorLayer" ), desc13 );
desc11.putObject( stringIDToTypeID( "using" ), idcontentLayer, desc12 );
executeAction( idmake, desc11, DialogModes.NO );
// =======================================================
    var desc2 = new ActionDescriptor();
    desc2.putClass( stringIDToTypeID( "new" ), idchannel );
        var ref1 = new ActionReference();
        ref1.putEnumerated( idchannel, idchannel, idmask = stringIDToTypeID( "mask" ) );
    desc2.putReference( stringIDToTypeID( "at" ), ref1 );
    desc2.putEnumerated( stringIDToTypeID( "using" ), stringIDToTypeID( "userMaskEnabled" ), stringIDToTypeID( "hideAll" ) );
executeAction( idmake, desc2, DialogModes.NO );
// =======================================================
    var desc3 = new ActionDescriptor();
        var desc4 = new ActionDescriptor();
            var ref2 = new ActionReference();
            ref2.putIndex( idchannel, theIndex );
        desc4.putReference( stringIDToTypeID( "to" ), ref2 );
        var idpreserveTransparency = stringIDToTypeID( "preserveTransparency" );
        desc4.putBoolean( idpreserveTransparency, true );
    desc3.putObject( stringIDToTypeID( "with" ), stringIDToTypeID( "calculation" ), desc4 );
executeAction( idapplyImageEvent = stringIDToTypeID( "applyImageEvent" ), desc3, DialogModes.NO );
};

 

3 replies

c.pfaffenbichler
Community Expert
Community Expert
May 3, 2020
// create layers for channels that are not empty;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var theChannels = collectChannels ();
// create layers;
for (var m = theChannels.length-1; m >= 0; m--) {
loadAndLayer (theChannels[m][1], theChannels[m][0], [0,0,0]);
};
};
////// collect layers //////
function collectChannels () {
// get number of layers;
var ref = new ActionReference();
//ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfChannels'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfChannels"));
// pixel dimensions;
var theWidth = applicationDesc.getUnitDoubleValue(stringIDToTypeID("width"));
var theHeight = applicationDesc.getUnitDoubleValue(stringIDToTypeID("height"));
var theRes = applicationDesc.getUnitDoubleValue(stringIDToTypeID("resolution"));
var thePixels = (theWidth*theRes/72)*(theHeight*theRes/72);
// process the channels;
var theChannels = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex(stringIDToTypeID( "channel" ), m);
var channelDesc = executeActionGet(ref);
// collect values;
var theName = channelDesc.getString(stringIDToTypeID('channelName'));
var theIndex = channelDesc.getInteger(stringIDToTypeID('itemIndex'));
var theID = channelDesc.getInteger(stringIDToTypeID('ID'));
if (channelDesc.hasKey(stringIDToTypeID('alphaChannelOptions')) == true) {
var theHistogram = channelDesc.getList(stringIDToTypeID('histogram'));
var value0 = theHistogram.getInteger(0);
// if not completely black;
if (value0 != thePixels) {
theChannels.push([theName, theIndex, theID])
} else {}
};
} catch (e) {};
};
return theChannels
};
//////
function loadAndLayer (identifier, theName, theColor) {
var idnull = stringIDToTypeID( "null" );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idchannel = stringIDToTypeID( "channel" );
// =======================================================
    var desc10 = new ActionDescriptor();
        var ref2 = new ActionReference();
        ref2.putProperty( idchannel, stringIDToTypeID( "selection" ) );
    desc10.putReference( idnull, ref2 );
        var ref3 = new ActionReference();
        ref3.putIndex( idchannel, identifier );
    desc10.putReference( stringIDToTypeID( "to" ), ref3 );
executeAction( stringIDToTypeID( "set" ), desc10, DialogModes.NO );
// =======================================================
    var desc11 = new ActionDescriptor();
        var ref4 = new ActionReference();
        ref4.putClass( idcontentLayer );
    desc11.putReference( idnull, ref4 );
        var desc12 = new ActionDescriptor();
            var desc13 = new ActionDescriptor();
                var desc14 = new ActionDescriptor();
                desc14.putDouble( stringIDToTypeID( "red" ), theColor[0] );
                desc14.putDouble( stringIDToTypeID( "grain" ), theColor[1] );
                desc14.putDouble( stringIDToTypeID( "blue" ), theColor[2] );
            var idRGBColor = stringIDToTypeID( "RGBColor" );
            desc13.putObject( stringIDToTypeID( "color" ), idRGBColor, desc14 );
        desc12.putString( stringIDToTypeID( "name" ), theName );
        desc12.putObject( stringIDToTypeID( "type" ), stringIDToTypeID( "solidColorLayer" ), desc13 );
        desc11.putObject( stringIDToTypeID( "using" ), idcontentLayer, desc12 );
    executeAction( stringIDToTypeID( "make" ), desc11, DialogModes.NO );
};
Kukurykus
Legend
May 3, 2020

What should I do to run your script without error on this line: 

var channelDesc = executeActionGet(ref);
c.pfaffenbichler
Community Expert
Community Expert
May 3, 2020

Well, I may have done insuffiient testing – just plain RGB images with alpha-channels, spot channel, … 

Could you post a screenshot of the Channels Panel for the file with which this error occurs? 

 

And I didn’t use the putProperty-technique here because I wanted to retrieve more than just the

numberOfChannels

 but also the image’s dimensions. 

 

Edit: 

JJMack
Community Expert
Community Expert
May 3, 2020

Alpha channels are like selection and layer mask a gray scale channel. All white would be like a select all black like no selection. Converted to a layer they would Gray scale images  that have three channel RGB if the document mode is RGB. One would Target an alpha select all copy to the clipboard and paste in a layer that you should mate tp be associated with the Alpha channel. You would still have up to 53 Alpha channels and up to 53 additional RGB images layers. Would you also want the alpha channels deleted? Or do you was soe other kinf of conversion? Why do you need to convert a channel to an image layer?  Hers a Loade and imags lunionsity as as slecetion. Is saved the selection as Alpha Image Lininiosity.  It targeted the Alpjt channel. Then did menu Select all, Menu edit>copy   Menu edit>Paste.  You can see Layer 1 looks like Alpha Image Luminisoty and has a Red Green and Blue channel. I just don't undetstand what reasion the woul be to do duch a thing.

JJMack
c.pfaffenbichler
Community Expert
Community Expert
May 3, 2020

Could you please post a screenshot with the pertinent Panels (Toolbar, Layers, Channels, Options Bar, …) visible for the source and the intended result?