2 Replies Latest reply on Nov 9, 2012 12:24 PM by adam@blueapplestudio

    Batch convert .png to .tga and retain alpha

    adam@blueapplestudio Level 1

      I'm looking for a way to save out .png files as targas and retain their alphas.  I'm writing in Javascript and couldn't figure how to access and manipulate the .png's alpha successfully using the documentation, so I gave the Script Listener plugin a shot.  Unfortunately my "manual" method for saving out a .png to .tga invloves:

       

      CTRL-Clicking the layer to make a selection

      Making a new alpha based on the selection

      Duplicating the layer and merging down a bunch (this makes semi transparent edge pixels retain all of their color when flattened)

      Flattening the image

      Saving as tga 32 with alpha

       

      The script log is very long, and throws errors when run.  I suspect the first offending move it makes is trying to CTRL-Click a layer to grab a selection.

       

      Is there a smarter approach to handling this?

       

      Incidentally, if File>Export>Render Video could make a targa sequence that preserved the alpha channel in an .avi the above .png to .tga conversion wouldn't be needed.

       

      Any assistance is appreciated.

       

      Code below.  The first offending line is "executeAction(idsetd,desc2,DialogModes.NO);" about 1/3 of the way down.

       

      //folder select dialog
      var inputFolder = Folder.selectDialog("Select a folder to process");
      //========================================================================================
      //--1.5--Convert png files to targas with alpha and save
      var fileList = inputFolder.getFiles();
      //for each file in the list
      for (i = 0; i < fileList.length; i++)
      {
       //check to see if the current file is a targa or avi, and run the appropriate function
       var sFileName = fileList[i].name;
       var sPNG = ".png";
       var sPNGcaps = ".PNG";
       //Performed if png
       if ((sFileName.indexOf(sPNG) != -1) || (sFileName.indexOf(sPNGcaps) != -1))
       {
        // =======================================================
        var idsetd = charIDToTypeID("setd");
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref1 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idfsel = charIDToTypeID("fsel");
        ref1.putProperty(idChnl,idfsel);
        desc2.putReference(idnull,ref1);
        var idT = charIDToTypeID("T   ");
        var ref2 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idTrsp = charIDToTypeID("Trsp");
        ref2.putEnumerated(idChnl,idChnl,idTrsp);
        desc2.putReference(idT,ref2);
       
       
       
        //PROBLEM BELOW!
        executeAction(idsetd,desc2,DialogModes.NO);
       
       
       
       
        // =======================================================
        var idDplc = charIDToTypeID("Dplc");
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref3 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idfsel = charIDToTypeID("fsel");
        ref3.putProperty(idChnl,idfsel);
        desc3.putReference(idnull,ref3);
        executeAction(idDplc,desc3,DialogModes.NO);
        // =======================================================
        var idsetd = charIDToTypeID("setd");
        var desc4 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref4 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idfsel = charIDToTypeID("fsel");
        ref4.putProperty(idChnl,idfsel);
        desc4.putReference(idnull,ref4);
        var idT = charIDToTypeID("T   ");
        var idOrdn = charIDToTypeID("Ordn");
        var idNone = charIDToTypeID("None");
        desc4.putEnumerated(idT,idOrdn,idNone);
        executeAction(idsetd,desc4,DialogModes.NO);
        // =======================================================
        for (k = 0; k < 50; k++)
        {
         var idCpTL = charIDToTypeID("CpTL");
         executeAction(idCpTL,undefined,DialogModes.NO);
        }
        // =======================================================
        //flatten image
        var idFltI = charIDToTypeID("FltI");
        executeAction(idFltI,undefined,DialogModes.NO);
        // =======================================================
        var currentFileName = app.activeDocument.name;
        var currentFilePath = inputFolder;
        var newFileNameAndPath = currentFilePath + "/" + currentFileName + ".tga";
        //save as tga with alpha start
        var idsave = charIDToTypeID("save");
        var desc58 = new ActionDescriptor();
        var idAs = charIDToTypeID("As  ");
        var desc59 = new ActionDescriptor();
        var idBtDp = charIDToTypeID("BtDp");
        desc59.putInteger(idBtDp,32);
        var idCmpr = charIDToTypeID("Cmpr");
        desc59.putInteger(idCmpr,0);
        var idTrgF = charIDToTypeID("TrgF");
        desc58.putObject(idAs,idTrgF,desc59);
        var idIn = charIDToTypeID("In  ");
        desc58.putPath(idIn,new File(newFileNameAndPath));
        var idDocI = charIDToTypeID("DocI");
        desc58.putInteger(idDocI,310);
        var idCpy = charIDToTypeID("Cpy ");
        desc58.putBoolean(idCpy,true);
        var idAlpC = charIDToTypeID("AlpC");
        desc58.putBoolean(idAlpC,false);
        var idsaveStage = stringIDToTypeID("saveStage");
        var idsaveStageType = stringIDToTypeID("saveStageType");
        var idsaveBegin = stringIDToTypeID("saveBegin");
        desc58.putEnumerated(idsaveStage,idsaveStageType,idsaveBegin);
        executeAction(idsave,desc58,DialogModes.NO);
        // ......................
        var idsave = charIDToTypeID("save");
        var desc60 = new ActionDescriptor();
        var idAs = charIDToTypeID("As  ");
        var desc61 = new ActionDescriptor();
        var idBtDp = charIDToTypeID("BtDp");
        desc61.putInteger(idBtDp,32);
        var idCmpr = charIDToTypeID("Cmpr");
        desc61.putInteger(idCmpr,0);
        var idTrgF = charIDToTypeID("TrgF");
        desc60.putObject(idAs,idTrgF,desc61);
        var idIn = charIDToTypeID("In  ");
        desc60.putPath(idIn,new File(newFileNameAndPath));
        var idDocI = charIDToTypeID("DocI");
        desc60.putInteger(idDocI,310);
        var idCpy = charIDToTypeID("Cpy ");
        desc60.putBoolean(idCpy,true);
        var idAlpC = charIDToTypeID("AlpC");
        desc60.putBoolean(idAlpC,false);
        var idsaveStage = stringIDToTypeID("saveStage");
        var idsaveStageType = stringIDToTypeID("saveStageType");
        var idsaveSucceeded = stringIDToTypeID("saveSucceeded");
        desc60.putEnumerated(idsaveStage,idsaveStageType,idsaveSucceeded);
        executeAction(idsave,desc60,DialogModes.NO);
        //save as tga with alpha end
        // =======================================================
        //close file
        var idCls = charIDToTypeID("Cls ");
        var desc62 = new ActionDescriptor();
        var idSvng = charIDToTypeID("Svng");
        var idYsN = charIDToTypeID("YsN ");
        var idN = charIDToTypeID("N   ");
        desc62.putEnumerated(idSvng,idYsN,idN);
        executeAction(idCls,desc62,DialogModes.NO);
       }
      }
      
      

       

      Message was edited by: adam@blueapplestudio

        • 1. Re: Batch convert .png to .tga and retain alpha
          Paul Riggott Level 6

          Would this work for you...

           

           

          main();
          function main(){
          var pngFolder = Folder.selectDialog( "Please select PNG folder"); 
          if(pngFolder == null) return;
          var outputFolder = Folder (pngFolder + "/Targa");
          if(!outputFolder.exists) outputFolder.create();
          var fileList = pngFolder.getFiles("*.png");
          for(var a in fileList){
          open(fileList[a]);
          var doc = activeDocument;
          var Name = doc.name.replace(/\.[^\.]+$/, '');
          loadSelection();
          try{
          var SB = doc.selection.bounds;
          var contentsChannel = doc.channels.add(); 
          contentsChannel.kind = ChannelType.SELECTEDAREA; 
          contentsChannel.name = 'Alpha'; 
          doc.selection.store(doc.channels['Alpha'], SelectionType.REPLACE); 
          doc.activeChannels = doc.componentChannels;
          contentsChannel.visible=false;
          doc.selection.deselect();
          }catch(e){}
          var saveFile = File(outputFolder + "/" + Name + ".tga");
          saveTarga(saveFile);
          doc.close(SaveOptions.DONOTSAVECHANGES);
              }
          };
          function loadSelection() {
              var desc51 = new ActionDescriptor();
                  var ref32 = new ActionReference();
                  ref32.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
              desc51.putReference( charIDToTypeID('null'), ref32 );
                  var ref33 = new ActionReference();
                  ref33.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
              desc51.putReference( charIDToTypeID('T   '), ref33 );
              desc51.putBoolean( charIDToTypeID('Invr'), true );
              executeAction( charIDToTypeID('setd'), desc51, DialogModes.NO );
          };
          function saveTarga(saveFile){
          targaSaveOptions = new TargaSaveOptions();
          targaSaveOptions.alphaChannels = true;
          targaSaveOptions.resolution = TargaBitsPerPixels.THIRTYTWO;
          activeDocument.saveAs(saveFile, targaSaveOptions, true, Extension.LOWERCASE);
          };
          
          
           
          • 2. Re: Batch convert .png to .tga and retain alpha
            adam@blueapplestudio Level 1

            @Paul Riggott- Thank you for your assistance.  Your script is excellent, however it saves out a pre-multiplied alpha and I need a straight, unmatted alpha.  To address this I duplicate the color layer 255 times and then flatten the image.  This is very inefficient but it gets thte job done.  If you or anyone else knows of a more efficient method to do this feel free to share.

             

            Also, this .png to .tga functionality is part of a larger piece I am working on, so I have modified a few elements to fit.

             

             

            main();
            function main(){
            var pngFolder = inputFolder; 
            if(pngFolder == null) return;
            var outputFolder = inputFolder;
            //if(!outputFolder.exists) outputFolder.create();
            var fileList = pngFolder.getFiles("*.png");
            for(var a in fileList){
            open(fileList[a]);
            var doc = activeDocument;
            var Name = doc.name.replace(/\.[^\.]+$/, '');
            loadSelection();
            try{
                var SB = doc.selection.bounds;
                var contentsChannel = doc.channels.add(); 
                contentsChannel.kind = ChannelType.SELECTEDAREA; 
                contentsChannel.name = 'Alpha'; 
                doc.selection.store(doc.channels['Alpha'], SelectionType.REPLACE); 
                doc.activeChannels = doc.componentChannels;
                contentsChannel.visible=false;
                doc.selection.deselect();
                }catch(e){}
                //+++++++++++    
                //duplicate layer
                for (i = 0; i < 255; i++){
                var idCpTL = charIDToTypeID( "CpTL" );
                executeAction( idCpTL, undefined, DialogModes.NO );
                }
                //flatten
                var idFltI = charIDToTypeID( "FltI" );
                executeAction( idFltI, undefined, DialogModes.NO );    
                //++++++++++++    
                var saveFile = File(outputFolder + "/" + Name + ".tga");
                saveTarga(saveFile);
                doc.close(SaveOptions.DONOTSAVECHANGES);
                }
            };
            function loadSelection() {
                var desc51 = new ActionDescriptor();
                    var ref32 = new ActionReference();
                    ref32.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
                desc51.putReference( charIDToTypeID('null'), ref32 );
                    var ref33 = new ActionReference();
                    ref33.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
                desc51.putReference( charIDToTypeID('T   '), ref33 );
                desc51.putBoolean( charIDToTypeID('Invr'), true );
                executeAction( charIDToTypeID('setd'), desc51, DialogModes.NO );
            };
            function saveTarga(saveFile){
            targaSaveOptions = new TargaSaveOptions();
            targaSaveOptions.alphaChannels = true;
            targaSaveOptions.resolution = TargaBitsPerPixels.THIRTYTWO;
            activeDocument.saveAs(saveFile, targaSaveOptions, true, Extension.LOWERCASE);
            };