FeastX

PngGroupSave

Jan 8th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. PTH = "C/Users/Feast/Desktop/Test"; displayDialogs = DialogModes.NO
  2. var Path = $.getenv('pth') || PTH || "d/";
  3.  
  4. // for saving files
  5. var doc = app.activeDocument;
  6.  
  7. var Name = doc.name.replace(/\.[^\.]+$/, '');
  8. var Prefix = "";
  9. var Suffix = "";
  10.  
  11. var BlurAmount = [25,50,100];
  12. var DivideScaleBlurAmount = [1.25,1.5,2];
  13.  
  14. var AddBlur = false;
  15. var AddVeg = false;
  16. var AddSP = false;
  17. var AllowNonGroupLayers = false;
  18.  
  19. var Res = 512;
  20.  
  21. var DocTrimmedWidth = 0;
  22. var DocTrimmedHeight = 0;
  23.  
  24. var BlurredTrimmedWidth = 0;
  25. var BlurredTrimmedHeight = 0;
  26.  
  27. var saveFile = File(Path + "/" + Name + Suffix + ".jpg");
  28.  
  29. var dlg = new Window('dialog', 'PNG Batch Save');
  30.  
  31. var G1 = dlg.add('group', undefined, '');
  32.  
  33.  
  34. dlg.msgSt = G1.add('statictext', undefined,
  35. 'Path:');
  36. dlg.msgSt.alignment = [ScriptUI.Alignment.LEFT,
  37. ScriptUI.Alignment.TOP]
  38. dlg.msgEt = G1.add('edittext', [0,0,500,20],
  39. Path, {name: "path",multiline:false,noecho:false,readonly:false});
  40.  
  41. dlg.msgSt = G1.add('statictext', undefined,
  42. 'Reso:');
  43. dlg.msgEt = G1.add('edittext', [0,0,40,20],
  44. Res, {name: "Res"});
  45.  
  46.  
  47. // Resolution UI
  48. dlg.msgPnl = dlg.add('panel', undefined, '');
  49. dlg.msgPnl.orientation = "row";
  50. dlg.titleSt = dlg.msgPnl.add('statictext', undefined,
  51. 'Prefix:');
  52. dlg.titleEt = dlg.msgPnl.add('edittext', [0,0,100,20],
  53. Prefix, {name: "Prefix"});
  54.  
  55. // Add a checkbox to control the buttons that dismiss an alert box
  56. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  57. 'SP', {name: "SP"});
  58. dlg.hasBtnsCb.value = true;
  59. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  60. 'B', {name: "Blur"});
  61. dlg.hasBtnsCb.value = true;
  62. // Add a checkbox to control the buttons that dismiss an alert box
  63. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  64. 'VG', {name: "Veg"});
  65. dlg.hasBtnsCb.value = false;
  66. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  67. 'Render Ungrouped', {name: "allowLayers"});
  68. dlg.hasBtnsCb.value = false;
  69.  
  70. dlg.msgPnl.btn = dlg.msgPnl.add('button', undefined, 'Export', {name:"export"});
  71. dlg.msgPnl.btn.onClick = Algorithm;
  72.  
  73. dlg.msgPnl.buildBtn = dlg.msgPnl.add('button', undefined, 'Cancel');
  74.  
  75.  
  76.  
  77. function key_handle(e)
  78. {
  79. switch (e.keyIdentifier)
  80. {
  81. case "Enter":
  82. //~ dlg.btnPnl.btn.notify();
  83. Algorithm();
  84. break;
  85. }
  86. }
  87.  
  88. dlg.msgEt.active = true;
  89.  
  90. dlg.addEventListener ("keydown", key_handle, false);
  91.  
  92. dlg.show();
  93.  
  94. // Algorithm must be called when clicking on Export button
  95. function Algorithm()
  96. {
  97. GetUIValues();
  98.  
  99. SetSuffix();
  100.  
  101. $.setenv('pth', Path.slice(1))
  102. if (PTH != ge = $.getenv('pth')) {
  103. (fle = File($.fileName)).open('r'), jsxFile = fle.read()
  104. .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close()
  105. fle.open('w'), fle.write(jsxFile), fle.close()
  106. }
  107.  
  108. dlg.close();
  109.  
  110. var doc = app.activeDocument;
  111. // need an array to know which grp was on or off
  112. var oldLayers = new Array();
  113. for (var i = 0; i < doc.layerSets.length; i++) {
  114. if (doc.layerSets[i].visible == true) {
  115. oldLayers.push(1);
  116. };
  117.  
  118. if (doc.layerSets[i].visible == false) {
  119. oldLayers.push(0); // not visible
  120. };
  121. doc.layerSets[i].visible = false;
  122. };
  123.  
  124. // set all non-groups layers off
  125. var oldNonGroupLayers = new Array();
  126. if (AllowNonGroupLayers == false)
  127. {
  128. for (var i = 0; i < doc.layers.length; i++) {
  129. if (doc.layers[i].visible == true) {
  130. oldNonGroupLayers.push(1);
  131. };
  132.  
  133. if (doc.layers[i].visible == false) {
  134. oldNonGroupLayers.push(0); // not visible
  135. };
  136. doc.layers[i].visible = false;
  137. }
  138. }
  139.  
  140. for (var i = 0; i < doc.layerSets.length; i++) {
  141. // if the layer used to be visible
  142. if (oldLayers[i] == 1) {
  143. //alert("Found a visible grp!");
  144. // we set it visible
  145. doc.layerSets[i].visible = true;
  146.  
  147. TrimResizeAndSave(doc.layerSets[i]);
  148.  
  149. if (AddBlur)
  150. {
  151. for (var j = 1; j <= BlurAmount.length; j++)
  152. {
  153. var currentBlur = BlurAmount[j-1];
  154.  
  155. CreateBlurredVersion(doc.layerSets[i], currentBlur, j, DivideScaleBlurAmount[j-1]);
  156. }
  157. }
  158.  
  159.  
  160. // we set it invisible again
  161. doc.layerSets[i].visible = false;
  162.  
  163. };
  164. }
  165.  
  166. // set whichever groups were originally on visible again
  167. for (var i = 0; i < doc.layerSets.length; i++) {
  168. if (oldLayers[i] == 1)
  169. doc.layerSets[i].visible = true;
  170. }
  171.  
  172. // set whichever non-group layers were originally on visible again
  173. if (AllowNonGroupLayers == false)
  174. {
  175. for (var i = 0; i < doc.layers.length; i++) {
  176. if (oldNonGroupLayers[i] == 1)
  177. doc.layers[i].visible = true;
  178. }
  179. }
  180.  
  181. //~ alert("Finished exporting files");
  182. }
  183.  
  184.  
  185. function GetUIValues() {
  186. Res = dlg.findElement("Res").text;
  187.  
  188. AddBlur = dlg.findElement("Blur").value;
  189. AddVeg = dlg.findElement("Veg").value;
  190. AddSP = dlg.findElement("SP").value;
  191. Prefix = dlg.findElement("Prefix").text;
  192. AllowNonGroupLayers = dlg.findElement("allowLayers").value;
  193.  
  194. Path = dlg.findElement("path").text;
  195.  
  196. var c = /\\/g;
  197. if (Path.match(c)) {
  198. //~ alert("found \\");
  199. Path = Path.replace(c, "/");
  200. };
  201.  
  202. c = ':';
  203. if (Path.match(c )) {
  204. //~ alert("found :");
  205. Path = Path.replace(c , '');
  206. };
  207.  
  208. Path = "/" + Path;
  209. }
  210.  
  211. function SaveJPEG(saveFile, jpegQuality) {
  212. jpgSaveOptions = new JPEGSaveOptions();
  213. jpgSaveOptions.embedColorProfile = true;
  214. jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  215. jpgSaveOptions.matte = MatteType.NONE;
  216. jpgSaveOptions.quality = jpegQuality;
  217. activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
  218. }
  219.  
  220. function SavePNG(saveFile) {
  221. pngSaveOptions = new PNGSaveOptions(9, false);
  222. activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
  223. }
  224.  
  225. function TrimResizeAndSave(group) {
  226. var doc = app.activeDocument;
  227. var savedState = doc.activeHistoryState;
  228.  
  229. // trim
  230. doc.trim(TrimType.TRANSPARENT, true, true, true, true);
  231.  
  232. // we need to find the current width and height of the picture now
  233. // this way we can find the right amount of pixels the picture should be blurred
  234. // to keep the blur even across all the pictures.
  235. // ========== set units to pixels ==========//
  236. var oldPrefs = app.preferences.rulerUnits;
  237. app.preferences.rulerUnits = Units.PIXELS;
  238. DocTrimmedWidth = doc.width;
  239. DocTrimmedHeight = doc.height;
  240.  
  241. // resize algorithm
  242. if (DocTrimmedHeight > DocTrimmedWidth) {
  243. doc.resizeImage(null, UnitValue(Res, "px"),72, ResampleMethod.BICUBIC, 100);
  244. }
  245. else {
  246. doc.resizeImage(UnitValue(Res, "px"), null, 72, ResampleMethod.BICUBIC, 100);
  247. }
  248.  
  249. app.preferences.rulerUnits = oldPrefs;
  250. // we save the ouput
  251. if (AddBlur)
  252. saveFile = File(Path + "/" + "-B0-" + Prefix + group.name + Suffix + ".png");
  253. else saveFile = File(Path + "/" + Prefix + group.name + Suffix + ".png");
  254. SavePNG(saveFile);
  255.  
  256. doc.activeHistoryState = savedState;
  257. }
  258.  
  259. function CreateBlurredVersion(group, blurStrength, number, scale)
  260. {
  261. var doc = app.activeDocument;
  262. var savedState = doc.activeHistoryState;
  263.  
  264. var blurredPath = File(Path + "/" + "-B" + number + "-" + Prefix + group.name + Suffix + ".png");
  265.  
  266. var theName = group.name;
  267. // we need to find the current width and height of the picture now
  268. // this way we can find the right amount of pixels the picture should be blurred
  269. // to keep the blur even across all the pictures.
  270. // ========== set units to pixels ==========//
  271. var oldPrefs = app.preferences.rulerUnits;
  272. app.preferences.rulerUnits = Units.PIXELS;
  273.  
  274. doc.activeHistoryState = savedState;
  275.  
  276. // transform document to smart object
  277. //~ var result = createSmartObject(group);
  278. var result = group.merge();
  279.  
  280. // gaussian blur
  281. //result.applyGaussianBlur(blurStrength, blurStrength);
  282. //~ (aD = activeDocument).activeLayer.applyGaussianBlur(blurStrength * (sqrt = Math.sqrt(aD.width * aD.height / Math.pow(512, 2))) + sqrt / Math.PI / aD.resolution * 72)
  283. //~ Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}}
  284. //~ Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}}
  285. var v2= DocTrimmedWidth * DocTrimmedHeight;
  286. Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 / sqrt(5000* 5000 / v2)}}}
  287. //~ if (number==3) alert("blurAmount of "+ theName + ": " + Math.blur(blurStrength, v2));
  288.  
  289. result.applyGaussianBlur(Math.blur(blurStrength, v2));
  290.  
  291. //~ var v1= Math.sqrt (doc.width * doc.height);
  292. //~ var blurFactor = v1 / v2;
  293. //~ alert("DocTrimmedWidth: " + DocTrimmedWidth);
  294. //~ alert("DocTrimmedWidth: " + DocTrimmedHeight);
  295. //~ alert("doc.width: " + doc.width);
  296. //~ alert("doc.height: " + doc.height);
  297. //~ alert("blurFactor: " + blurFactor);
  298. //~ result.applyGaussianBlur(blurStrength * blurFactor);
  299. //~ var blurCalc = blurStrength * Math.sqrt(DocTrimmedWidth * DocTrimmedHeight / Math.pow((DocTrimmedWidth + DocTrimmedHeight)/2, 2));
  300. //~ result.applyGaussianBlur(blurCalc);
  301. //~ Math.blur(blurStrength, (doc.width + doc.height)/2);
  302.  
  303.  
  304.  
  305. doc.trim(TrimType.TRANSPARENT, true, true, true, true);
  306. BlurredTrimmedWidth = doc.width;
  307. BlurredTrimmedHeight = doc.height;
  308.  
  309. // resize algorithm
  310. if (BlurredTrimmedHeight > BlurredTrimmedWidth) {
  311. doc.resizeImage(null, UnitValue(Res/ scale, "px"), 72, ResampleMethod.BICUBIC, 100);
  312. }
  313. else {
  314. doc.resizeImage(UnitValue(Res/ scale, "px"), null, 72, ResampleMethod.BICUBIC, 100);
  315. }
  316.  
  317. app.preferences.rulerUnits = oldPrefs;
  318.  
  319. // save
  320. SavePNG(blurredPath);
  321. doc.activeHistoryState = savedState;
  322. }
  323.  
  324. // create smartobject from specified layer or layerSet/group
  325. function createSmartObject(layer)
  326. {
  327. var doc = app.activeDocument;
  328. var layer = layer != undefined ? layer : doc.activeLayer;
  329.  
  330. if(doc.activeLayer != layer) doc.activeLayer = layer;
  331.  
  332. try
  333. {
  334. var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
  335. executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
  336. return doc.activeLayer;
  337. }
  338. catch(e)
  339. {
  340. return undefined;
  341. }
  342. }// JavaScript Document
  343.  
  344.  
  345. function SetSuffix()
  346. {
  347. if (AddSP && AddVeg)
  348. Suffix = "-VG-SP";
  349. else if (AddSP)
  350. Suffix = "-SP";
  351. else if (AddVeg)
  352. Suffix = "-VG";
  353. }
Advertisement
Add Comment
Please, Sign In to add comment