11 Replies Latest reply on Jun 29, 2011 7:19 PM by UCAudio

    merge 2 files for masking with alpha channels

    klaas_riverwoods

      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,

       

       

      test.jpg

        • 1. Re: merge 2 files for masking with alpha channels
          c.pfaffenbichler Level 9

          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?

          • 2. Re: merge 2 files for masking with alpha channels
            klaas_riverwoods Level 1

            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

            • 3. Re: merge 2 files for masking with alpha channels
              Paul Riggott Level 6

              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[i];
                  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();
              
              1 person found this helpful
              • 4. Re: merge 2 files for masking with alpha channels
                klaas_riverwoods Level 1

                This was great!!!!!

                It work's perfect for us - except that the png format makes things way too heavy.

                I know nothing about scripting...

                 

                do you think we could change this so we could start with jpg's and end up with a final png?

                Or even better for me...a transparent tif?

                 

                thanx a lot greetings,

                Klaas

                • 5. Re: merge 2 files for masking with alpha channels
                  klaas_riverwoods Level 1

                  I adapated the orignal script - and ended up with a png but....with a jpg extension..

                  that's a little stupid.

                   

                  I am not able to find how to correct this...

                   

                  My adaptions in red...please help

                   

                  //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('*.jpg');

                    for (var i = 0; i < pngs.length; i++) {
                      var file = pngs[i];
                      var afile = new File(folder + '/' + file.name.replace('.jpg', 'a.jpg'));
                      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();

                  • 6. Re: merge 2 files for masking with alpha channels
                    c.pfaffenbichler Level 9

                    »file.name« includes ».jpg«.

                    • 7. Re: merge 2 files for masking with alpha channels
                      klaas_riverwoods Level 1

                      I'm sorry - Are you just confirming something...or do I have to 'include" this jpg string somewhere?

                      To be honest I must say I'm not a scripter at all.

                       

                      To most prefferd situation would be that I start from to jpgs and end up with a transparent tiff.

                      That would be Ideal!

                       

                      Untill now I simply converted my jpgs to pngs so it would fit in to the script.

                      But this is a way too slow and heavy process.

                      On top of that is png not ideal to work with in indesign which is the end program I'm using.

                       

                      sorry to bother you guys with this,

                      and my thanx

                      • 8. Re: merge 2 files for masking with alpha channels
                        c.pfaffenbichler Level 9

                        You never seem to make the instruction to remove »jpg« from the name and instead use »png«.

                        • 9. Re: merge 2 files for masking with alpha channels
                          klaas_riverwoods Level 1

                          Oh ic,

                           

                          thanx, that is becauseI don't know how to do that....

                          This just an existing script I adapted...

                           

                          How do I do that?

                          • 10. Re: merge 2 files for masking with alpha channels
                            c.pfaffenbichler Level 9

                            You could change the save options to something like:

                            tifOpts = new TiffSaveOptions() ;      
                            tifOpts.embedColorProfile = true;      
                            tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
                            tifOpts.alphaChannels = false;      
                            tifOpts.byteOrder = ByteOrder.MACOS;
                            tifOpts.layers = true;
                            

                             

                            And the export-line to

                            var docName = doc.name;
                            var basename = docName.match(/(.*)\.[^\.]+$/)[1];
                            
                            doc.saveAs((new File(ofolder + '/' + basename + '.tif')),tifOpts,true)
                            doc.close(SaveOptions.DONOTSAVECHANGES);
                            

                             

                            And if you think it would be easier if I just amended the Script, that might be so, but you may pick up more this way, I guess.

                            • 11. Re: merge 2 files for masking with alpha channels
                              UCAudio

                              Hey, this thread is really helpful to me... thank you! I was wondering if it would be possible to modify this script to work in my situation. Instead of having 100 jpgs and 100 mask png files... I have 100 jpgs and then just 1 mask png that I want to apply to all 100 jpgs and then have the resulting 100 images be saved as png files with alpha transparency. If someone could help me with this it would be very much appreciated. Thanks!