What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Batch save for 4096+ variable images - SERIOUS GURU NEEDED


achromatichouse

New Member
Messages
3
Likes
0
Hello, I'm a graphic designer who has been given quite a weighty task. I have an image of a man who is broken into 4 separate parts; head, torso, legs, arms. Each of his separate parts are in a group layer containing 8 different color variations of said parts (ie. in ARMS group folder, there is a layer of red arms, blue arms, etc.).

I NEED to have flattened jpgs of every single possible color combination, which equals out to 4,096 variations. I wanted to know if there is some sort of way i can tell photoshop to run through each of those variations and save all 4,096 files as fully flattened jpgs. I've already done a similar project with only 2 variations, turning layers on and off and saving the things myself, trying to remember naming conventions, and it was a nightmare. Trying to avoid that nightmare again - PLEASE HELP

Thank you for your time
 
Hi, and welcome.
If you have Photoshop CS4 or CS5 this script should do what you want.
Copy and paste the script into ExtendScript Toolkit, this gets installed with Photoshop
This utility can be found in the relevant folder:-
PC: C:\Program Files\Adobe\Adobe Utilities
MAC: <hard drive>/Applications/Utilities/Adobe Utilities
The script needs to be saved into:-
PC: C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts
NB: Vista and Windows 7 you will have to save it elsewhere and copy it to this folder due to permissions.
MAC: <hard drive>/Applications/Adobe Photoshop CS#/ Presets/Scripts
If Photoshop was open, close and restart Photoshop so that it can pickup the new script.
Code:
function main(){
if(activeDocument.layerSets.length != 4) {
    alert("You do not have four layersets!");
    return;
    }
outputFolder = Folder.selectDialog("Please select the output folder");
if(outputFolder == null) return;
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Parts0 =[];
var Parts1 =[];
var Parts2 =[];
var Parts3 =[];
for(var a =0 ;a<activeDocument.layerSets[0].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[0].layers[a];
    Parts0.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[1].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[1].layers[a];
    Parts1.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[2].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[2].layers[a];
    Parts2.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[3].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[3].layers[a];
    Parts3.push(getActiveLayerIndex());
    }
var layerSel=[];
for(var a in Parts0){
var p0 = Parts0[a];
    for(var b in Parts1){
       var p1 = Parts1[b]; 
       for(var c in Parts2){
           var p2 = Parts2[c]; 
           for(var d in Parts3){
               var p3 = Parts3[d]; 
               layerSel.push([[p0],[p1],[p2],[p3]]);
               }
           }
        }
 }
for(var z in layerSel){
 makeActiveByIndex( Number(layerSel[z][0]));
 makeActiveByIndex( Number(layerSel[z][1]),true);
 makeActiveByIndex( Number(layerSel[z][2]),true);
 makeActiveByIndex( Number(layerSel[z][3]),true);
 dupLayers("Temp");
 var saveFile = new File(outputFolder  + "/" + Name +"-" + zeroPad((z+1), 4) +".jpg");
 SaveForWeb(saveFile,80);
 app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
}
main();
function getActiveLayerIndex() {
   var ref = new ActionReference();
   ref.putProperty( 1349677170 , 1232366921 );
   ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
   var res = executeActionGet(ref).getInteger( 1232366921 ) - Number( hasBackground() );
   return res;   
};
function hasBackground(){
    var res = undefined;
    try{
        var ref = new ActionReference();
        ref.putProperty( 1349677170 , 1315774496); 
        ref.putIndex( 1283027488, 0 );
        executeActionGet(ref).getString(1315774496 );; 
        res = true;
    }catch(e){ res = false}
    return res;
};
function makeActiveByIndex( idx, add ){ 
    add == undefined ? add = false : add = true;
    var desc = new ActionDescriptor(); 
      var ref = new ActionReference(); 
      ref.putIndex(charIDToTypeID( "Lyr " ), idx) 
      desc.putReference( charIDToTypeID( "null" ), ref ); 
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
};
function dupLayers(NewDocName) { 
    var desc143 = new ActionDescriptor();
        var ref73 = new ActionReference();
        ref73.putClass( charIDToTypeID('Dcmn') );
    desc143.putReference( charIDToTypeID('null'), ref73 );
    desc143.putString( charIDToTypeID('Nm  '), NewDocName );
        var ref74 = new ActionReference();
        ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc143.putReference( charIDToTypeID('Usng'), ref74 );
    executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO );
};
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb(); 
   sfwOptions.format = SaveDocumentType.JPEG; 
   sfwOptions.includeProfile = false; 
   sfwOptions.interlaced = 0; 
   sfwOptions.optimized = true; 
   sfwOptions.quality = jpegQuality; //0-100 
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
function zeroPad(n, s) { 
n = n.toString(); 
while (n.length < s) n = '0' + n; 
return n; 
};
To use:-
The PSD must be open in Photoshop
The PSD must only have four layersets (groups)
File - Scripts - select the script
It will prompt you for an output folder, once you have selected this everthing is done for you.
The files will be put in the output folder with a name of DocumentName-0001.jpg etc.
All the best, Serious Guru:rolleyes:
 
your posts simply put me in Awe Paul MR....... Damn you bgood! with that stuff
 
Paul MR, you are a life saver! This is sheer brilliance, I can't even believe this was actually possible!! THANK YOU THANK YOU THANK YOU THANK YOU!!!!!

You're the man!
 
Hi again Paul MR, just one more quick question for you:

If I wanted to add a 5th variable, would I just copy the various parts such as

for(var d in Parts3){
var p3 = Parts3[d];

and the various other lines like that and change it to Parts4 and "e"?

Thank you so much for all you've done, I really really wholeheartedly absolutely appreciate it!
 
yes, here is the full script..
Code:
function main(){
if(activeDocument.layerSets.length != 5) {
    alert("You do not have five layersets!");
    return;
    }
outputFolder = Folder.selectDialog("Please select the output folder");
if(outputFolder == null) return;
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Parts0 =[];
var Parts1 =[];
var Parts2 =[];
var Parts3 =[];
var Parts4 =[];
for(var a =0 ;a<activeDocument.layerSets[0].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[0].layers[a];
    Parts0.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[1].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[1].layers[a];
    Parts1.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[2].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[2].layers[a];
    Parts2.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[3].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[3].layers[a];
    Parts3.push(getActiveLayerIndex());
    }
for(var a =0 ;a<activeDocument.layerSets[4].layers.length;a++){
    activeDocument.activeLayer= activeDocument.layerSets[4].layers[a];
    Parts4.push(getActiveLayerIndex());
    }
var layerSel=[];
for(var a in Parts0){
var p0 = Parts0[a];
    for(var b in Parts1){
       var p1 = Parts1[b]; 
       for(var c in Parts2){
           var p2 = Parts2[c]; 
           for(var d in Parts3){
               var p3 = Parts3[d]; 
               for(var e in Parts4){
                   var p4 = Parts4[e]; 
               layerSel.push([[p0],[p1],[p2],[p3],[p4]]);
                    }
               }
           }
        }
 }
for(var z in layerSel){
 makeActiveByIndex( Number(layerSel[z][0]));
 makeActiveByIndex( Number(layerSel[z][1]),true);
 makeActiveByIndex( Number(layerSel[z][2]),true);
 makeActiveByIndex( Number(layerSel[z][3]),true);
 makeActiveByIndex( Number(layerSel[z][4]),true);
 dupLayers("Temp");
 var saveFile = new File(outputFolder  + "/" + Name +"-" + zeroPad((z+1), 4) +".jpg");
 SaveForWeb(saveFile,80);
 app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
}
main();
function getActiveLayerIndex() {
   var ref = new ActionReference();
   ref.putProperty( 1349677170 , 1232366921 );
   ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
   var res = executeActionGet(ref).getInteger( 1232366921 ) - Number( hasBackground() );
   return res;   
};
function hasBackground(){
    var res = undefined;
    try{
        var ref = new ActionReference();
        ref.putProperty( 1349677170 , 1315774496); 
        ref.putIndex( 1283027488, 0 );
        executeActionGet(ref).getString(1315774496 );; 
        res = true;
    }catch(e){ res = false}
    return res;
};
function makeActiveByIndex( idx, add ){ 
    add == undefined ? add = false : add = true;
    var desc = new ActionDescriptor(); 
      var ref = new ActionReference(); 
      ref.putIndex(charIDToTypeID( "Lyr " ), idx) 
      desc.putReference( charIDToTypeID( "null" ), ref ); 
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
};
function dupLayers(NewDocName) { 
    var desc143 = new ActionDescriptor();
        var ref73 = new ActionReference();
        ref73.putClass( charIDToTypeID('Dcmn') );
    desc143.putReference( charIDToTypeID('null'), ref73 );
    desc143.putString( charIDToTypeID('Nm  '), NewDocName );
        var ref74 = new ActionReference();
        ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc143.putReference( charIDToTypeID('Usng'), ref74 );
    executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO );
};
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb(); 
   sfwOptions.format = SaveDocumentType.JPEG; 
   sfwOptions.includeProfile = false; 
   sfwOptions.interlaced = 0; 
   sfwOptions.optimized = true; 
   sfwOptions.quality = jpegQuality; //0-100 
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
function zeroPad(n, s) { 
n = n.toString(); 
while (n.length < s) n = '0' + n; 
return n; 
}
Just run the code and go for a coffee
:)
 

Back
Top