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 11, 2022

»What is confusing is the "Backstory" link you sent IS to that previous topic I posted, which was just a few days before.«

@Stephen Marsh posted that link, not me. 

 

»All the images would be in the same directory.«

What would the naming convention of the actual rendered images be? 

»Star0001«, »Star-Mask_0001«, »NGon-Mask_0001« or different from that? 


Yeah.  Again, you are correct.  I need to look closer.  His @4801628 confused me as to who was saying what.

As a rendered animation sequence, the part before the "_" could be anything.  I'd probably do something like PRD0300.tif to PRD0450.tif for the main image, then PRDm#### for it's mask, then who knows for the subtraction images, perhaps PRDx####.  And you'll notice there is no "_" in those namings.  It's the overall how-to I'm trying to accomplish.  Seems like what the files are named wouldn't really matter.

 

I'm sure you all are wondering why any of this is necessary.  In 3D animation, you sometimes need things visible for the render, but you cannot use the mask directly from the renders because some of the 3D objects visible in the render should not be part of the comp. They were necessary to get a correct render (because of things like reflections, ambient occlusion or whatever).  So to get a correct alpha for compositing, I render a separate, quick pass forcing pure black and white on objects and rendering the same camera range to an 8bit file sequence. I'll then need to replace the alpha that came with the original render.  Or the 8bit sequence needs to be subtracted from the existing alpha in the image.   A situation where this may come up is in compositing tricky cross sections or windows into the interior of a 3D object.  It may be way too complicated to get the 3D scene set up with a lot of layered nuance hard coded (so-to-speak) in the rendering, so you render it in passes and get to where you need to be in post.  And then sometimes you need to monkey with the alpha to get it to comp correctly.

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