Copy link to clipboard
Copied
So I've got myself wrapped around the axle here trying to understand channels. I can't get my head around the fact that you can duplicate, add and remove, but you can't copy and paste channel contents? I'm working in JS in CS3. Or trying to.
I'm trying to take a gray image with three channels, and reduce it to two. Copy channel 2 into channel 1, copy channel 3 into 2, then delete channel 3 and save the file.
Why doesn't this work? I can copy and paste from artlayers so the method exists, but for whatever reason I can't get it to accept this method for channels:
// copy Alpha 1 and paste it into Gray
app.activeDocument.channel[2].copy();
app.activeDocument.channel[1].paste();
// copy Alpha 2 and paste it into Alpha 1
app.activeDocument.channel[3].copy();
app.activeDocument.channel[2].paste();
// Delete old Alpha 2 channel
app.activeDocument.channel[3].remove();
I tried another way - setting the active channel then copying and pasting, but I couldn't even get it to set the active channel without an error:
app.activeDocument.activeChannels = app.activeDocument.channel[2];
Very stuck and in need of help. Thanks.
It's not working for lots of reasons. Channels, and almost everything else start with 0. To use an index you have to use Channels. The Channel class does not have a copy method. And finally activeChannels expects an array.
So it's easy fixes for everything except the copy method. For that we need to use scriptlistner to make an applyImage function. When you apply a image in normal mode it's the same as copy without going through the clipboard.
...// assumes grayscale mode with two alpha channels
// ma
Copy link to clipboard
Copied
It's not working for lots of reasons. Channels, and almost everything else start with 0. To use an index you have to use Channels. The Channel class does not have a copy method. And finally activeChannels expects an array.
So it's easy fixes for everything except the copy method. For that we need to use scriptlistner to make an applyImage function. When you apply a image in normal mode it's the same as copy without going through the clipboard.
// assumes grayscale mode with two alpha channels
// make gray channel active
app.activeDocument.activeChannels = [app.activeDocument.channels[0]];
// copy first alpha contents to gray
applyChannelByName( app.activeDocument.channels[1].name );
// make first alpha channel active
app.activeDocument.activeChannels = [app.activeDocument.channels[1]];
// copy second alpha content to frist alpha
applyChannelByName( app.activeDocument.channels[2].name );
// remove the second alpha
app.activeDocument.channels[2].remove();
// make gray channel active
app.activeDocument.activeChannels = [app.activeDocument.channels[0]];
function applyChannelByName( name ) {
var desc = new ActionDescriptor();
var applyDesc = new ActionDescriptor();
var ref = new ActionReference();
ref.putName( charIDToTypeID('Chnl'), name);
applyDesc.putReference( charIDToTypeID('T '), ref );
applyDesc.putBoolean( charIDToTypeID('PrsT'), true );
desc.putObject( charIDToTypeID('With'), charIDToTypeID('Clcl'), applyDesc );
executeAction( charIDToTypeID('AppI'), desc, DialogModes.NO );
}
Copy link to clipboard
Copied
Mike - good answer. It's solved my issue and I've been able to carry on with the rest of the script function. Thanks for that. I've learned a little more about PS scripting today.
Copy link to clipboard
Copied
Is this what you are trying to do?
if(activeDocument.mode == DocumentMode.RGB){
selectChannel('green');
activeDocument.selection.selectAll();
activeDocument.selection.copy();
selectChannel('red');
activeDocument.paste();
selectChannel('blue');
activeDocument.selection.selectAll();
activeDocument.selection.copy();
selectChannel('green');
activeDocument.paste();
activeDocument.selection.deselect();
selectChannel('RGB');
//If you remove the blue channel you will be left with a multichannel not RGB file
// You will have Cyan and Magenta channels
//activeDocument.activeChannels[2].remove();
}
function selectChannel (colour){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), stringIDToTypeID(colour) );
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
};
Edit: Mike was much faster than me and a better solution.
Copy link to clipboard
Copied
Thanks Paul, but you bring up a good point. I should have a test for mode and number of channels to avoid throwing errors. I just tried my code on an RGB image to see what would happen and the apply image function doesn't work in that mode. Even if it did, removing a component channel is not something you would want to do without a very good reason.
Copy link to clipboard
Copied
if (activeDocument.mode == DocumentMode.GRAYSCALE && activeDocument.channels.length == 3) {
app.activeDocument.activeChannels = [app.activeDocument.channels[0]];
app.activeDocument.channels[0].remove();
// Now Multi channel Doc
app.activeDocument.changeMode(ChangeMode.GRAYSCALE);
// Alpha 1 has become composite Gray
app.activeDocument.channels[1].name = 'Alpha 1';
}
Just my option…
Copy link to clipboard
Copied
If your document is grayscale you only have one channel Mark.
Copy link to clipboard
Copied
1 composite yes + ? alphas in the above I was testing for 2…
Copy link to clipboard
Copied
Mark is correct - the images I'm working with end up with two alpha channels in the resulting grayscale image - three channels which need reducing to two. I should point out that the images are being used in CGI so we use all sorts of weird channel combinations which would be "illegal" in the normal sense of an image because they simply make no sense - for example an RGB image with 8 channels 🙂
Copy link to clipboard
Copied
Mark,
If your code was to show that you could remove a component channel then convert the mode back, that is true. The reason I said you don't want to do that without a good reason is the conversion is not color managed when you delete component channels. It would be better to convert to multi, delete then convert back.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now