I ran the script last night while I was sleeping and it stripped the alpha channel from all my target images, but I think I know what happened.
I think your script is copying the Alpha channel from the source and applying it to the target. My source images ARE the alpha channel and don't have one of their own. So the RGB combined is the alpha channel in the source image, and I want that copied over to the alpha slot of the target images.
To your other comment about changing the file formats, I have another program for batch converting file formats so it's no big deal. I've attached an example of the Source and Target images just to illustrate.
@TorQueMoD2
I tested the following script based on the single set of 2 images that you have provided. Both source and target images should be in separate folders. The expectation is that both the source and target images are alphabetically sorted in the same order. The PNG quality level can be set via the saveOptions.compression line. You will be prompted for a new location to save to. The output will be PNG. Test with a small set of images to save time (not 3K).
You may wish to split up the 3K files into smaller batches of say 1K if you are happy with the tests. Photoshop can have problems processing large batches of images.
/*
Mask PNG Images with PNG Masks from 2 Separate Input Folders.jsx
v1.0 - 4th November 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-or-batch-tutorial-on-how-to-copy-alpha-channel-from-one-set-of-images-to-another/td-p/14205993
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-script-to-apply-a-series-of-alpha-channels-in-bulk-to-a-series-of-images/td-p/13642975
*/
#target photoshop
(function () {
if (app.documents.length === 0) {
try {
// Image input folder
var folder1 = Folder.selectDialog("Select the target/image folder:");
if (folder1 === null) {
//alert('Script cancelled!');
return;
}
// Mask input folder
var folder2 = Folder.selectDialog("Select the source/mask folder:");
if (folder2 === null) {
//alert('Script cancelled!');
return;
}
// Validate input folder selection
var validateInputDir = (folder1.fsName === folder2.fsName);
if (validateInputDir === true) {
alert("Script cancelled as both the input folders are the same!");
return;
}
// Limit the file input to PNG, case insensitive
var list1 = folder1.getFiles(/\.(png)$/i);
var list2 = folder2.getFiles(/\.(png)$/i);
// Alpha-numeric sort
list1.sort();
list2.sort();
// Validate that folder 1 & 2 lists are not empty
var validateEmptyList = (list1.length > 0 && list2.length > 0);
if (validateEmptyList === false) {
alert("Script cancelled as one of the input folders is empty!");
return;
}
// Validate that the item count in folder 1 & 2 matches
var validateListLength = (list1.length === list2.length);
if (validateListLength === false) {
alert("Script cancelled as the input folders don't have equal quantities of images!");
return;
}
// Output folder
var saveFolder = Folder.selectDialog("Please select the folder to save to...");
if (saveFolder === null) {
//alert('Script cancelled!');
return;
}
// Save and set the dialog display settings
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
// PNG save options
var saveOptions = new PNGSaveOptions();
saveOptions.compression = 0; // compression value: 0-9
saveOptions.interlaced = false;
// Set the file processing counter
var fileCounter = 0;
// Perform the stacking and saving
for (var i = 0; i < list1.length; i++) {
// Open the image and get the name
var doc = open(list1[i]);
var docName = doc.name.replace(/\.[^\.]+$/, '');
// Place the mask as a layer
placeFile(list2[i], 100);
// Load the mask RGB channel as a selection
channelSelection();
// Remove the mask layer
activeDocument.activeLayer.remove();
// Add a layer mask from the selection to the image
maskSelection("revealSelection");
// Rename layer to image doc name
activeDocument.activeLayer.name = docName;
// Save and close the PNG version
var savePNG = new File(new File(saveFolder + '/' + docName + '.png'), saveOptions);
doc.saveAs(savePNG, saveOptions, true, Extension.LOWERCASE);
doc.close(SaveOptions.DONOTSAVECHANGES);
// Increment the file processing counter for each file processed
fileCounter++;
}
// End of script
app.displayDialogs = savedDisplayDialogs;
app.beep();
alert('Script completed!' + '\r' + fileCounter + ' masked PNG files of ' + list1.length + ' input files saved to:' + '\r' + saveFolder.fsName);
} catch (e) {
alert("Error!" + "\r" + e + ' ' + e.line);
}
} else {
alert('Please close all open documents before running this script!');
}
///// Functions /////
function placeFile(file, scale) {
try {
var idPlc = charIDToTypeID("Plc ");
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc2.putPath(idnull, new File(file));
var idFTcs = charIDToTypeID("FTcs");
var idQCSt = charIDToTypeID("QCSt");
var idQcsa = charIDToTypeID("Qcsa");
desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
var idOfst = charIDToTypeID("Ofst");
var desc3 = new ActionDescriptor();
var idHrzn = charIDToTypeID("Hrzn");
var idPxl = charIDToTypeID("#Pxl");
desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
var idVrtc = charIDToTypeID("Vrtc");
var idPxl = charIDToTypeID("#Pxl");
desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
var idOfst = charIDToTypeID("Ofst");
desc2.putObject(idOfst, idOfst, desc3);
var idWdth = charIDToTypeID("Wdth");
var idPrc = charIDToTypeID("#Prc");
desc2.putUnitDouble(idWdth, idPrc, scale);
var idHght = charIDToTypeID("Hght");
var idPrc = charIDToTypeID("#Prc");
desc2.putUnitDouble(idHght, idPrc, scale);
var idAntA = charIDToTypeID("AntA");
desc2.putBoolean(idAntA, true);
executeAction(idPlc, desc2, DialogModes.NO);
} catch (e) {}
}
function channelSelection() {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putProperty(s2t("channel"), s2t("selection"));
descriptor.putReference(s2t("null"), reference);
reference2.putEnumerated(s2t("channel"), s2t("channel"), s2t("grain"));
descriptor.putReference(s2t("to"), reference2);
executeAction(s2t("set"), descriptor, DialogModes.NO);
}
function maskSelection(maskParameter) {
// Parameter = "revealSelection" or "hideSelection"
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
descriptor.putClass(s2t("new"), s2t("channel"));
reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("mask"));
descriptor.putReference(s2t("at"), reference);
descriptor.putEnumerated(s2t("using"), s2t("userMaskEnabled"), s2t(maskParameter));
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
}());
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html