-
-
Save lm913/5dce8b8f874343918042 to your computer and use it in GitHub Desktop.
Benchmark PhotoShop Actions in Extended Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var folder = 'C:\\LocalStorage'; // resource folder | |
var benchmarkFile = 'C:\\LocalStorage\\benchmark.csv'; // test results | |
/** | |
* The functions you combine in this method will be benchmark tested | |
* @param fileName - Target file | |
*/ | |
function psAction(fileName){ | |
psOpenFile(fileName); | |
psDefaultUnits(); | |
psFit(1000, 1500, 300); | |
psSaveTiff(); | |
psCloseFile(); | |
} | |
function psOpenFile(fileName){ | |
// Only want to open non-hidden files (and no folders) | |
if ((fileName instanceof File) && (fileName.hidden == false)) { | |
// Open in Photoshop | |
open(fileName); | |
} | |
} | |
function psCloseFile(){ | |
var doc = activeDocument; | |
doc.close(SaveOptions.SAVECHANGES); | |
} | |
function psDefaultUnits(){ | |
app.preferences.rulerUnits = Units.PIXELS | |
app.preferences.typeUnits = TypeUnits.PIXELS | |
app.preferences.askBeforeSavingLayeredTIFF = false | |
app.preferences.maximizeCompatibility = true | |
app.foregroundColor.rgb.hexValue = "000000" | |
app.backgroundColor.rgb.hexValue = "ffffff" | |
} | |
function psAddLayers(nLayers){ // run on tif files | |
var doc = activeDocument; | |
for(var i = 0; i < nLayers; i++){ | |
doc.backgroundLayer.duplicate(); | |
} | |
} | |
function psSaveTiff(){ | |
var doc = activeDocument; | |
var tiffSaveOptions = new TiffSaveOptions(); | |
tiffSaveOptions.byteOrder = ByteOrder.MACOS; | |
tiffSaveOptions.embedColorProfile = true; | |
tiffSaveOptions.layers = true; | |
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW; | |
tiffSaveOptions.layerCompression = LayerCompression.RLE; | |
doc.saveAs(doc.path, tiffSaveOptions, false, Extension.LOWERCASE); | |
} | |
function psResize(percentage){ | |
var doc = activeDocument; | |
doc.resizeImage(UnitValue(percentage,"%"), UnitValue(percentage,"%"), // width, height | |
null, ResampleMethod.AUTOMATIC); // dpi | |
} | |
function psFlatten(){ | |
var doc = activeDocument; | |
doc.flatten(); | |
} | |
/** | |
* @param profile - The new color profile, such as: sRGB IEC61966-2.1 | |
*/ | |
function psRecolor(profile){ | |
var doc = activeDocument; | |
doc.convertProfile(profile, Intent.RELATIVECOLORIMETRIC, true, true ); | |
} | |
/* Fit image to desired canvas dimensions in pixels. | |
* Will not resize images smaller than the shortest starting document side | |
*/ | |
function psFit(endWidth, endHeight, endRes){ | |
var doc = activeDocument; | |
var startWidth = doc.width; | |
var startHeight = doc.height; | |
var startRes = doc.resolution; | |
app.displayDialogs = DialogModes.NO; | |
var startRatio = startWidth / startHeight; | |
var endRatio = endWidth / endHeight; | |
doc.backgroundLayer.duplicate(); | |
if(startRes != endRes){ | |
doc.resizeImage(startWidth, startHeight, endRes, ResampleMethod.AUTOMATIC); | |
} | |
if (endRatio < startRatio){ | |
if (endHeight > endWidth){ | |
doc.resizeCanvas(null, (1 / endRatio) * startHeight, AnchorPosition.MIDDLECENTER); | |
} | |
} else { | |
doc.resizeCanvas(endRatio * startWidth, null, AnchorPosition.MIDDLECENTER); | |
} | |
} | |
function psExit(){ | |
app.purge(PurgeTarget.ALLCACHES); | |
executeAction(app.charIDToTypeID('quit'), undefined, DialogModes.NO); | |
} | |
function writeToFile(fileName, text){ | |
var txtFile = new File(fileName); | |
txtFile.open("a"); | |
txtFile.writeln(text); | |
txtFile.close(); | |
} | |
function initFile(fileName){ | |
var txtFile = new File(fileName); | |
txtFile.open("w"); | |
txtFile.writeln("File,Size(Bytes),Time(ms)"); | |
txtFile.close(); | |
} | |
var inputFolder = new Folder(folder); | |
var fileList = inputFolder.getFiles(/\.(jpg|tif|psd|bmp|gif|png|jpeg|tiff)$/i); | |
var startTime, endTime, actionTime; | |
initFile(benchmarkFile); | |
for (var i = 0; i < fileList.length; i++) { | |
startTime = new Date().getTime(); | |
// Perform PhotoShop action on File | |
psAction(fileList[i]); | |
endTime = new Date().getTime(); | |
actionTime = endTime - startTime; // in milliseconds | |
writeToFile(benchmarkFile, fileList[i].name + "," + | |
fileList[i].length + "," + | |
actionTime); | |
} | |
psExit(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SCRIPTS_FOLDER = 'c:\\Path\\To\\Script\\Folder\\'; | |
var script = new File(SCRIPTS_FOLDER + 'child.jsx'); // the child script | |
$.evalFile (script); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment