Skip to content

Instantly share code, notes, and snippets.

@kulmajaba
Last active September 8, 2022 10:39
Show Gist options
  • Save kulmajaba/53c460b0e5d2c3cbd3a57774e14ff63c to your computer and use it in GitHub Desktop.
Save kulmajaba/53c460b0e5d2c3cbd3a57774e14ff63c to your computer and use it in GitHub Desktop.
PhotoShop ExtendScript file to Photomerge selected files in pairs using reposition
// Create a file in the path "Adobe Photoshop 2022/Presets/Scripts/Xpan.jsx" and paste the contents there to use
// Xpan Stitcher, author Mika Kuitunen
var runphotomergeFromScript = true; // must be before Photomerge include
//@include "Photomerge.jsx"
//@show include
// DEBUGGING STUFF
function ConsoleLog() {
// Seeing logs: macOS: nc -lvk 8000, Win: install Ncat, then ncat -lk 8000
this.socket = new Socket();
this.hostPort = "127.0.0.1:8000";
}
ConsoleLog.prototype.log = function(logMessage){
if (!debug) return;
if (this.socket.open(this.hostPort)){
this.socket.write (logMessage + "\n");
this.socket.close();
}
};
var debug = false;
var console = new ConsoleLog();
// ----------------------------------------------------------------------------
// MAIN SCRIPT
// override Photomerge.jsx settings
// "Auto", "Prsp", "cylindrical", "spherical", "sceneCollage", "translation"
photomerge.alignmentKey = "translation";
photomerge.advancedBlending = true;
photomerge.lensCorrection = false;
photomerge.removeVignette = false;
photomerge.autoFillTrans = false;
// TIFF options
// https://theiviaxx.github.io/photoshop-docs/Photoshop/TiffSaveOptions.html
var tiffOpts = new TiffSaveOptions();
tiffOpts.imageCompression = TIFFEncoding.TIFFZIP;
tiffOpts.layers = false;
// Progress bar UI
function progress(steps) {
var progressBar;
var progressWindow = new Window("palette", "Progress", undefined, {closeButton: false});
var progressText = progressWindow.add("statictext");
progressText.preferredSize = [450, -1]; // 450 pixels wide, default height.
if (steps) {
progressBar = progressWindow.add("progressbar", undefined, 0, steps);
progressBar.preferredSize = [450, -1]; // 450 pixels wide, default height.
}
progress.close = function () {
progressWindow.close();
};
progress.increment = function () {
progressBar.value++;
};
progress.message = function (message) {
progressText.text = message;
};
progressWindow.show();
}
function main() {
var fList = File.openDialog("Select file pairs for stitching", undefined, true);
if (!fList) {
console.log("User aborted");
return;
}
if (fList.length % 2 !== 0) {
alert("Uneven number of files selected, aborting");
console.log("Abort mission");
return;
}
// Each file pair takes two steps, merging and saving
var progressSteps = fList.length;
var filePairTotal = fList.length / 2;
progress(progressSteps);
for (var i = 0; i < fList.length - 1; i += 2) {
var files = [fList[i], fList[i+1]];
console.log("Loop " + i + ", files " + files[0].name + ", " + files[1].name);
var currentFilePair = i / 2 + 1;
progress.message("Processing files " + files[0].name + ", " + files[1].name + " (" + currentFilePair + " of " + filePairTotal + ")");
photomerge.createPanorama(files, false);
progress.increment();
// The naming algorithm assumes names like "0032-01.CR3"
// The name is split at the dash into prefix and suffix
// The final name is constructed as "[prefix]-[file 1 suffix]-[file 2 suffix].tif"
var name = "";
for (var j = 0; j < files.length; j++) {
if (name === "") {
name = files[j].displayName.split(".").shift();
} else {
name += "-" + files[j].displayName.split(".").shift().split("-").pop();
}
}
// Save result one level above the source files if possible
var path = fList[0].parent.path || fList[0].path;
console.log("Saving " + path + "/" + name + ".tif");
progress.message("Saving ..." + path.slice(-30) + "/" + name + ".tif");
activeDocument.saveAs( new File( path + "/" + name ) , tiffOpts, true, Extension.LOWERCASE );
activeDocument.close( SaveOptions.DONOTSAVECHANGES );
progress.increment();
}
progress.close();
console.log("Done");
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment