Batch convert .png to .tga and retain alpha
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 savevar 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.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
