Skip to main content
Participating Frequently
April 6, 2011
Question

merge 2 files for masking with alpha channels

  • April 6, 2011
  • 1 reply
  • 1548 views

Dear all,

I have like hundreds of files. Each file has it's own "sister black&white file" which is ment to be used as a mask.

I was wondering if it is possible to automaticly merge those 2 files so we would end up with a masked image..png or whatever....

It would be a monster job to do it all by hand...

Maybe a a script or badge???

kind regards,

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
April 6, 2011

What are the naming-rules and the files’ locations (side by side?) and is the Alpha a grayscale image or RGB, too?

Edit: Could you post a pair of files for testing?

Participating Frequently
April 6, 2011

the alpha image will be rgb.

The filenaming is not problem - that we can take care of...

in other words - you can call it whatever you want...

thanx

Paul Riggott
Inspiring
April 6, 2011

Xbytor wrote a script to do this a while ago....


//add alpha to doc by X
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };

function main() {

  var folder = Folder.selectDialog();
  if (!folder) {
    return;
  }

  var ofolder = new Folder(folder + "/Converted");
  ofolder.create();

  var saveOpts = new ExportOptionsSaveForWeb();
  saveOpts.format = SaveDocumentType.PNG;
  saveOpts.PNG8 = false;
  saveOpts.transparency = true;

  var pngs = folder.getFiles('*.png');

  for (var i = 0; i < pngs.length; i++) {
    var file = pngs;
    var afile = new File(folder + '/' + file.name.replace('.png', 'a.png'));
    if (!afile.exists) {
      continue;
    }

    var doc = app.open(file);
    doc.activeLayer.isBackgroundLayer = false;
    var adoc = app.open(afile);
    adoc.selection.selectAll();
    adoc.selection.copy();
    adoc.close(SaveOptions.DONOTSAVECHANGES);

    createLayerMask();
    selectMaskChannel();
    doc.paste();

    var ofile = new File(ofolder + '/' + file.name);
    ofile.remove();
    doc.exportDocument(ofile, ExportType.SAVEFORWEB, saveOpts);
    doc.close(SaveOptions.DONOTSAVECHANGES);
  }
};
function createLayerMask() {
    var desc9 = new ActionDescriptor();
    desc9.putClass( cTID('Nw  '), cTID('Chnl') );
        var ref6 = new ActionReference();
        ref6.putEnumerated( cTID('Chnl'), cTID('Chnl'), cTID('Msk ') );
    desc9.putReference( cTID('At  '), ref6 );
    desc9.putEnumerated( cTID('Usng'), cTID('UsrM'), cTID('RvlA') );
    executeAction( cTID('Mk  '), desc9, DialogModes.NO );
};
function selectMaskChannel() {
    var desc26 = new ActionDescriptor();
        var ref18 = new ActionReference();
        ref18.putEnumerated( cTID('Chnl'), cTID('Chnl'), cTID('Msk ') );
        ref18.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    desc26.putReference( cTID('null'), ref18 );
    desc26.putBoolean( cTID('MkVs'), true );
    executeAction( cTID('slct'), desc26, DialogModes.NO );
};

main();