Copy link to clipboard
Copied
I have a list of 3,000 png images with no alpha channel, and then another series of images of only the alpha channel and I really don't want to have to manually combine them. Is there a script or other way to automate this process? I've tried to search Google, but I keep finding very specific scripts to do this and save as a PSD or what have you, and nothing that works for my particular situation.
I essentially want to end up with 1 set of 3k png files with no background (masked from the alpha images).
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 th
...Copy link to clipboard
Copied
Please provide 3 sets of sample images (6 files) for testing. Don't alter the file names, I need to see the naming pattern. Ideally from around the beginning, middle and end of the batch.
Are the files grouped into a single directory or two separate folders?
Do you want to save another separate folder full of another 3K files, or overwrite the current files without backgrounds?
Edit:
As you have 3K of images, you might wish to consider using a faster CLI approach:
http://www.imagemagick.org/discourse-server/viewtopic.php?t=30341
https://imagemagick.org/Usage/masking/
Copy link to clipboard
Copied
The following script will batch copy alphas, but it will not automagically create a transparent background... You will either need to add extra code to the script or batch process the result of the script with a batch action or other script to create transparency from the alpha and save as PNG.
https://www.marspremedia.com/software/photoshop/copy-alpha-channels
Note: As Photoshop doesn't retain the alpha channel in PNG, you would first need to batch convert the PNG to TIFF or PSD which do support alphas.
Copy link to clipboard
Copied
Awesome! Thank you so much. Strange that there's a requirement for the file names to be the same. Thank God I already bought a batch file renaming program!
I'll let you know if this works. I'm assuming it will save to TGA fine? This is my preferred format for images with alpha channels.
Copy link to clipboard
Copied
The screenshot shows 3 filename options for the Mars Premedia script.
I think that you will need a custom script if the input and output files are PNG.
You really don't wish to save 3K files to another like TGA, TIFF or PSD format from PNG, just to remove the backgrounds using the matching alpha, then save another 3K PNG files again!
The following would need tweaks for your situation as it was for JPEG images, it's rare that such scripts apply to different situations:
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Always work on a small set of duplicates for testing!
You didn't answer my previous questions. So again...
Please provide 3 sets of sample images (6 files) for testing. Don't alter the file names, I need to see the naming pattern. Ideally from around the beginning, middle and end of the batch.
Are the files grouped into a single directory or two separate folders?
Do you want to save another separate folder full of another 3K files, or overwrite the current files without backgrounds?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thank you very much! Greatly appreciated. I'll take a look first thing tomorrow 🙂
Copy link to clipboard
Copied
@TorQueMoD2 - You're welcome! It will come down to the undisclosed/unknown file name sorting in the source and target folders.
P.S. The script could also use Save for Web rather than Save As to write the PNG files.