Skip to main content
Known Participant
June 6, 2019
Answered

Automate focus stacking script/action help needed.

  • June 6, 2019
  • 1 reply
  • 3426 views

Hi guys,

    this is what I am dealing with:

    yesterday I shot about 200 products, 3 shots/per product using focus stacking.

Filenames are in exact sequence:  filename001, filename002, filename003 are 3 different focus shots of the same product.

The next three files sequence ex: filename004.tif  filename005.tif filename006.tif are the shots of the next product etc.

So there is a folder with 600 shots which I have to repeat an action, that :

     a) Open three first shots and plays an action or script (any)

     b) then saves a new one image with a filename something like filename003-new.tif  for the first three, filename005-new.tif for the next... etc

     c) then repeat the action for the next three shots until the folder ends.

The result would be an image that had Auto-align the three images into a new one as layers and then Auto blend> stack images.

Any ideas?

This topic has been closed for replies.
Correct answer SuperMerlin

You could try this script, it prompts for the folder where the files are, creates a folder off this called Processed where the tif file are saved.

#target photoshop;

app.bringToFront();

main();

function main(){

var selectedFolder = Folder.selectDialog("Please select the folder to process");   

if(selectedFolder == null ) return;

var outFolder = Folder(selectedFolder + "/processed");

if(!outFolder.exists) outFolder.create();

var threeFiles = new Array();

var PictureFiles = selectedFolder.getFiles(/\.(jpg|jpe|dng|bmp|tif|tiff|psd|crw|cr2|exr|pcx|nef|dcr|dc2|erf|raf|orf|tga|mos|pef|png)$/i);

while(PictureFiles.length>2){

for(var a = 0;a<3;a++){threeFiles.push(PictureFiles.shift());}

stackFiles(threeFiles);

selectAllLayers();

autoAlign();

autoBlendLayers();

var layerName = activeDocument.activeLayer.name.replace(/\....$/i,'');

var saveFile = new File(outFolder+ '/' + layerName + '.psd');

SaveTIFF(saveFile);

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

threeFiles=[];

    }

};

function autoBlendLayers(){

var d=new ActionDescriptor();

d.putEnumerated(stringIDToTypeID("apply"), stringIDToTypeID("autoBlendType"), stringIDToTypeID("maxDOF"));

d.putBoolean(stringIDToTypeID("colorCorrection"), true);

d.putBoolean(stringIDToTypeID("autoTransparencyFill"), false);

executeAction(stringIDToTypeID("mergeAlignedLayers"), d, DialogModes.NO);

};

function SaveTIFF(saveFile){

tiffSaveOptions = new TiffSaveOptions();

tiffSaveOptions.embedColorProfile = true;

tiffSaveOptions.alphaChannels = true;

tiffSaveOptions.layers = true;

tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;

activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);

};

function selectAllLayers() {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc.putReference( charIDToTypeID('null'), ref );

executeAction( stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO );

};

function stackFiles(sFiles){ 

var loadLayersFromScript = true; 

var SCRIPTS_FOLDER =  decodeURI(app.path + '/' + localize('$$$/ScriptingSupport/InstalledScripts=Presets/Scripts'));

$.evalFile( new File(SCRIPTS_FOLDER +  '/Load Files into Stack.jsx'));  

loadLayers.intoStack(sFiles); 

};

function autoAlign() {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc.putReference( charIDToTypeID('null'), ref );

desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('ADSt'), stringIDToTypeID('ADSContent') );

desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('projection'), charIDToTypeID('Auto') );

desc.putBoolean( stringIDToTypeID('vignette'), false );

desc.putBoolean( stringIDToTypeID('radialDistort'), false );

executeAction( charIDToTypeID('Algn'), desc, DialogModes.NO );

};

function autoBlend() {

var desc = new ActionDescriptor();

desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('autoBlendType'), stringIDToTypeID('maxDOF') );

desc.putBoolean( charIDToTypeID('ClrC'), true );

executeAction( stringIDToTypeID('mergeAlignedLayers'), desc, DialogModes.NO );

};

1 reply

SuperMerlin
SuperMerlinCorrect answer
Inspiring
June 7, 2019

You could try this script, it prompts for the folder where the files are, creates a folder off this called Processed where the tif file are saved.

#target photoshop;

app.bringToFront();

main();

function main(){

var selectedFolder = Folder.selectDialog("Please select the folder to process");   

if(selectedFolder == null ) return;

var outFolder = Folder(selectedFolder + "/processed");

if(!outFolder.exists) outFolder.create();

var threeFiles = new Array();

var PictureFiles = selectedFolder.getFiles(/\.(jpg|jpe|dng|bmp|tif|tiff|psd|crw|cr2|exr|pcx|nef|dcr|dc2|erf|raf|orf|tga|mos|pef|png)$/i);

while(PictureFiles.length>2){

for(var a = 0;a<3;a++){threeFiles.push(PictureFiles.shift());}

stackFiles(threeFiles);

selectAllLayers();

autoAlign();

autoBlendLayers();

var layerName = activeDocument.activeLayer.name.replace(/\....$/i,'');

var saveFile = new File(outFolder+ '/' + layerName + '.psd');

SaveTIFF(saveFile);

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

threeFiles=[];

    }

};

function autoBlendLayers(){

var d=new ActionDescriptor();

d.putEnumerated(stringIDToTypeID("apply"), stringIDToTypeID("autoBlendType"), stringIDToTypeID("maxDOF"));

d.putBoolean(stringIDToTypeID("colorCorrection"), true);

d.putBoolean(stringIDToTypeID("autoTransparencyFill"), false);

executeAction(stringIDToTypeID("mergeAlignedLayers"), d, DialogModes.NO);

};

function SaveTIFF(saveFile){

tiffSaveOptions = new TiffSaveOptions();

tiffSaveOptions.embedColorProfile = true;

tiffSaveOptions.alphaChannels = true;

tiffSaveOptions.layers = true;

tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;

activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);

};

function selectAllLayers() {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc.putReference( charIDToTypeID('null'), ref );

executeAction( stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO );

};

function stackFiles(sFiles){ 

var loadLayersFromScript = true; 

var SCRIPTS_FOLDER =  decodeURI(app.path + '/' + localize('$$$/ScriptingSupport/InstalledScripts=Presets/Scripts'));

$.evalFile( new File(SCRIPTS_FOLDER +  '/Load Files into Stack.jsx'));  

loadLayers.intoStack(sFiles); 

};

function autoAlign() {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc.putReference( charIDToTypeID('null'), ref );

desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('ADSt'), stringIDToTypeID('ADSContent') );

desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('projection'), charIDToTypeID('Auto') );

desc.putBoolean( stringIDToTypeID('vignette'), false );

desc.putBoolean( stringIDToTypeID('radialDistort'), false );

executeAction( charIDToTypeID('Algn'), desc, DialogModes.NO );

};

function autoBlend() {

var desc = new ActionDescriptor();

desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('autoBlendType'), stringIDToTypeID('maxDOF') );

desc.putBoolean( charIDToTypeID('ClrC'), true );

executeAction( stringIDToTypeID('mergeAlignedLayers'), desc, DialogModes.NO );

};

2adpsAuthor
Known Participant
June 7, 2019

You are my Hero!

It worked like a charm!

Congrats!

Did u code the script?

SuperMerlin
Inspiring
June 7, 2019

Yes, but I chose 3 pics that were simular for a test and the result was very unusual