Skip to main content
Known Participant
April 7, 2022
Answered

Batching Alpha Channel Manipulation On Image Sequence

  • April 7, 2022
  • 3 replies
  • 3819 views

Thanks to this community, I learned how to work on the alpha channel, adding, replacing and performing subtraction calculations between two masks to create a new alpha channel.  Now the hard part (at least I think it will be), batching those operations on an image sequence.  I've found PS batching to be very difficult in the past, compared to the QFX Queue (see included image).

 

In the queue seen in the image, with these few lines of script, it opens a file, loads an alpha and saves a new file.  If it said FileAlphaSubtract instead of FileAlphaLoad, an 8bit mask image would be boolean subtracted from the existing image.  The #### in the script replaces the numerical image sequence with the Start, Inc, Times values.  Running the script is blazingly fast.  I'm always marveled at how quickly it must be doing these operations AND writting a file to disk.

 

Now to Photoshop.  As an animator compositing my renderings in Premier, I often do one of these two batch operations to an image sequence for use in my composite; add an alpha sequence to an image sequence or subtract an alpha sequence from an image sequence.  Say I have Test0000.tif to Test0300.tif.  I also have TMask0000.tif to TMask0300.tif.  So the PS batching needs to automate what just learnd to do manually.  Open an image, open the mask image, copy the mask image, paste it into the alpha channel of the main image, save to a new file, close the open images, repeat for the entire sequence.

 

Even more difficult, Open an image that already has alpha, open a mask image, added an additional channel in the first image, copy paste the mask image into the additional channel, run Calculations to subtract Aplha 2 from Alpha 1 creating a new channel, delete the Alpha 1 & 2 channels, then saving the file to a new image, and repeat for the entire sequence.

 

Just a few lines of script in QFX.  I have a feeling this is not so simple in PS.

Thanks and best regards.

 

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

Star0001.tif is a 24bit tif.  Inserting Star-Mask_0001.tif as Star0001,tif's new Alpha Channel, then saving to say... StarComp0001.tif (32bit) is an example of the first half.

 

There is another set in the series I included in the zip not shown above.  Star-Alpha_0001.tif is a 32bit star image with the proper mask included.  It is the exact completed version of the fist part above.  Taking that as a start, then replacing it's Alpha 1 channel with NGon-Mask_0001.tif and saving it to say... StarComp0001.tif (32bit) is an example of the second half.


This should work if the Mask-file’s name is the same as the RGB-file’s with »-Mask_« before the numbers.

 

Edited the Script 2022-04-14: 

 

// insert one channel from file with »-mask_« in otherwise identical name;
// 2022, use it at your own risk;
var theFolder = Folder("/Archiv HD/Pfaffi HD1/pfaffi_privat/photoshop/test/untitled folder 6");
if (theFolder.exists == false) {var theFolder = Folder.selectDialog ("select folder")};
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(tif|psd)$/i)
};
////////////////////////////////////
var theRegExp = new RegExp(/\d*\.\D{1,4}$/i);
var theRegExp2 = new RegExp(/\-*mask\_*\d*\.\D{1,4}$/i);
// process files;
for (var m = 0; m < theFiles.length; m++) {
    var thisFile = File(theFiles[m]);
    var thisName = thisFile.name;
    var aName = thisFile.name.replace(theRegExp, "");
    var aSuffix = thisFile.name.match(theRegExp);
// check for name similarities;
    for (var n = 0; n < theFiles.length; n++) {
// except the file itself;
        if (n != m) {
// check the name;            
        if (theFiles[n].name.match(theRegExp2, "") != null && theFiles[n].name.replace(theRegExp2, "") == aName && String(theFiles[n].name.match(theRegExp)) == aSuffix) {
                var mask1 = app.open(theFiles[n]);
                var theImage = app.open(File(thisFile));
                doCalculation (mask1.name, mask1.name, "normal", false, false);
// save tif;
                saveCopyAsTif (theImage, theImage.path+"/"+aName+"Comp"+aSuffix);
                mask1.close(SaveOptions.DONOTSAVECHANGES);
                theImage.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
        };
    };
////////////////////////////////////
////// perform calculation //////
function doCalculation (source1, source2, blendMode, invert1, invert2) {
var idcalculation = stringIDToTypeID( "calculation" );
var idchannel = stringIDToTypeID( "channel" );
var idordinal = stringIDToTypeID( "ordinal" );
var idtargetEnum = stringIDToTypeID( "targetEnum" );
var idto = stringIDToTypeID( "to" );
var idlayer = stringIDToTypeID( "layer" );
var idbackground = stringIDToTypeID( "background" );
var iddocument = stringIDToTypeID( "document" );
////////////////////////////////////
var desc4 = new ActionDescriptor();
    desc4.putClass( stringIDToTypeID( "new" ), idchannel );
        var desc5 = new ActionDescriptor();
            var ref1 = new ActionReference();
            ref1.putEnumerated( idchannel, idordinal, idtargetEnum );
            ref1.putProperty( idlayer, idbackground );
            ref1.putName( iddocument, source1 );
        desc5.putReference( idto, ref1 );
        desc5.putBoolean( stringIDToTypeID( "invert" ), invert1 );
        desc5.putEnumerated( idcalculation, stringIDToTypeID( "calculationType" ), stringIDToTypeID( blendMode ) );
        desc5.putDouble( stringIDToTypeID( "scale" ), 1.000000 );
        desc5.putInteger( stringIDToTypeID( "offset" ), 0 );
            var ref2 = new ActionReference();
            ref2.putEnumerated( idchannel, idordinal, idtargetEnum );
            ref2.putProperty( idlayer, idbackground );
            ref2.putName( iddocument, source2 );
        desc5.putReference( stringIDToTypeID( "source2" ), ref2 );
        desc5.putBoolean( stringIDToTypeID( "invertSource2" ), invert2 );
    desc4.putObject( stringIDToTypeID( "using" ), idcalculation, desc5 );
executeAction( stringIDToTypeID( "make" ), desc4, DialogModes.NO );
};
////// save tif //////
function saveCopyAsTif (myDocument, thePath) {
// tif options;
    tifOpts = new TiffSaveOptions();
    tifOpts.embedColorProfile = true;
    tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
    tifOpts.alphaChannels = true;
    tifOpts.byteOrder = ByteOrder.MACOS;
    tifOpts.layers = false;
// save copy;
    myDocument.saveAs((new File(thePath)), tifOpts, true);
};

 

 

 

3 replies

buck-wAuthor
Known Participant
April 8, 2022

I've set up small series of images that we can work with to try batching in PS.  The dropbox direct download link is:

https://dl.dropboxusercontent.com/s/sjqwlw3j63yiabw/PS%20Alpha%20Batch_.zip?dl=0

It is a six frame sequence of a 5 pointed star rotating clockwise.  There is also a pentagon inside the star rotating counter-clockwise (the mask for one anyway).

 

Star0001 to Star0006 is the star without any alpha.  This is for the batching of alpha insertion.  Star-Mask0001 to Star-Mask0006 in the alpha to be inserted.  The screen grab series of Alpha Insert images show me doing this for the first image in the series.  It shows; 01 - Adding an alpha channel to the 24bit star image, 02 - Copying the entire 8bit Ngon image, 03 - Pasting that image into the Star images blank alpha channel.  Not shown, but obvious, is to save the image to a new 32bit file (Star Comp0001.tif), then close the file. The goal is to batch this process for the entire sequence.

 

Next, and more complicated, is shown in the Alpha Subtract series of screen grabs.  They show; 01 - Opening the 32bit image Star Alpha, which already has it's alpha channel as shown, 02 - Creating a 2nd alpha channel, 03 - Copying the entire 8bit NGon image (selection not shown), 04 - Pasting the Ngon image into the Star image's second alpha channel, 05 - Using Calculations to subtract the Ngon alpha from the Star alpha, 06 - Deleting Alpha 1 & 2 channels with the booleaned alpha channel remaining.  Not shown is to save the image to a new 32bit file (Star Comp0001.tif), then close the file. The goal is to batch this process for the entire sequence.

 

There you have it.  I am used to the simplicity of doing this in QFX, however I'd like to up my PS game and be able to do it there.

 

Thanks for looking at this.

 

c.pfaffenbichler
Community Expert
Community Expert
April 10, 2022

Why would you want to waste resources with copying and pasting instead of using Calculations straight-up? 

buck-wAuthor
Known Participant
April 10, 2022

I am not a deft user of Photoshop.  You know this as you were involved in my previous post where I learned the process of inserting alpha, replacing alpha and subtracting alphas just the day before I made the Batching post (the next step).  Asking me why I don't do "X" is like asking a toddler why his calculus is so poor.

 

I'm happy to know better ways of doing things.  Did what you have shown above work only because of the simplicity of the RGB content of my example files?  If this was a more realistic and nuanced set of images as rendered in a PBR 3D environment, would what you did even work?

Stephen Marsh
Community Expert
Community Expert
April 8, 2022

@buck-w wrote:

Say I have Test0000.tif to Test0300.tif.  I also have TMask0000.tif to TMask0300.tif.  So the PS batching needs to automate what just learnd to do manually.  Open an image, open the mask image, copy the mask image, paste it into the alpha channel of the main image, save to a new file, close the open images, repeat for the entire sequence.

 

Even more difficult, Open an image that already has alpha, open a mask image, added an additional channel in the first image, copy paste the mask image into the additional channel, run Calculations to subtract Aplha 2 from Alpha 1 creating a new channel, delete the Alpha 1 & 2 channels, then saving the file to a new image, and repeat for the entire sequence.

 

Just a few lines of script in QFX.  I have a feeling this is not so simple in PS.

Thanks and best regards.


 

This will take scripting, and yes, it will be more than a few lines of code.

 

As suggested in the other topic thread... A download link to a file-sharing site would be best, such as your Adobe CC shares, DropBox, OneDrive, GoogleDrive or one of many other services.

 

At least 2 sets of images would be helpful, as would understanding if you have separate input folders and if file and alpha channel names are consistent.

 

buck-wAuthor
Known Participant
April 8, 2022

Sample images and descrption posted below.

c.pfaffenbichler
Community Expert
Community Expert
April 7, 2022

Please provide a better description of the processes and naming conventions, accompanied by meaningful screenshots (including the pertinent Panels). 

 

By Alpha Channel do you mean a plain non-composite Channel or Transparency? 

Your use copy/pasting seems a bit curious as Calculations can include multiple files anyway. 

Stephen Marsh
Community Expert
Community Expert
April 8, 2022