RGB / Multiple Image Sizes / Naming Conventions

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

jdsevier

RGB / Multiple Image Sizes / Naming Conventions

Post by jdsevier »

Hello,
I am very fluent with Photoshop until it comes to scripting.
I have been successful in creating and using actions but, it has defiantly been trial and error (mostly error)
I am currently working on a print catalog for a costume manufacturer. They need to have all the costumes (after retouching and color corrections) cropped and scaled down to specific pixel dimensions for a new web site.
All the sizes are proportionate to each other. I would like to keep the resolution set to 72dpi, just for consistency.

1) All images will need to be converted to RGB

2) Four sizes for each image: All dimensions are in pixels.

1242x1656 (Zoom Image) I will receive this size image from the designer, with the correct cropping, pixel dimensions and name.
414x552 (Large Image)
165x220 (Catalog Image)
66x88 (Thumbnail Image)

Below is the example needed for naming convention:
3) Five images of each costume (different views) *001 will be replaced with the Style Name or Number of the costume.

MAIN IMAGE: ("Hero Shot" on web page)
1242x1656 (Zoom Image) Naming will be: *001_mainz.jpg
414x552 (Large Image) Naming will be: *001_main.jpg
165x220 (Catalog Image) Naming will be: *001_cat.jpg
66x88 (Thumbnail Image) Naming will be: *001_mainthumb.jpg

DETAIL IMAGE 1:
1242x1656 (Zoom Image) Naming will be: *001_alt1z.jpg
414x552 (Large Image) Naming will be: *001_alt1.jpg
66x88 (Thumbnail Image) Naming will be: *001_alt1thumb.jpg

DETAIL IMAGE 2:
1242x1656 (Zoom Image) Naming will be: *001_alt2z.jpg
414x552 (Large Image) Naming will be: *001_alt2.jpg
66x88 (Thumbnail Image) Naming will be: *001_alt2thumb.jpg

DETAIL IMAGE 3:
1242x1656 (Zoom Image) Naming will be: *001_alt3z.jpg
414x552 (Large Image) Naming will be: *001_alt3.jpg
66x88 (Thumbnail Image) Naming will be: *001_alt3thumb.jpg

DETAIL IMAGE 4:
1242x1656 (Zoom Image) Naming will be: *001_alt4z.jpg
414x552 (Large Image) Naming will be: *001_alt4.jpg
66x88 (Thumbnail Image) Naming will be: *001_alt4thumb.jpg

4) "Saved for Web" as High quality jpegs

After typing this out, it looks as if I will need two separate scripts:
One for the "Main Images" and one for the "Detail Images" since the detail images do not get a Catalog size.
Or maybe its five different scripts because of the naming specifics of each one?

I could easily make an action to give me the RGB conversion and correct sizes but, where I get lost is in the naming conventions.
It would be great to have a script I could type in the correct Style Name or Number on the first save then have it repeat that same name, while automatically changing the the suffix to the correct description.
Please let me know if this can be done, I would love to learn how to do this.
Also, If you need any more information or examples I could certainly set some up

Thanks in advance.
-Jeff
pfaffenbichler

RGB / Multiple Image Sizes / Naming Conventions

Post by pfaffenbichler »

This should handle the main image; but I fail to understand how the detail-images are to be recognized prior to operation so I don’t know how to set up a conditionality for those.
Are they named with some special suffix?

Code: Select all// saves resized jpg copies;
// existing files of same name will be replaced without prompt;
// 2011, use it at your own risk;
#target photoshop
// set to pixels;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
// check for open document of correct size;
if (app.documents.length > 0 && app.activeDocument.width == 1242 && app.activeDocument.height == 1656) {
var myDocument = app.activeDocument;
// thanks to xbytor;
var documentName = myDocument.name;
var basename = documentName.match(/(.*)\.[^\.]+$/)[1];
var documentPath = myDocument.path;
// create copy;
var theCopy = myDocument.duplicate ("copy", true);
// set to 72ppi which is actually useless as saveforweb will strip this information;
theCopy.resizeImage (undefined, undefined, 72, ResampleMethod.NONE);
// convert to srgb;
theCopy.convertProfile ("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, true)
// resize;
if (theCopy.width > 600) {
   theCopy.resizeImage (600, undefined, 72, ResampleMethod.BICUBIC)
   }
else {
   theCopy.resizeImage (undefined, undefined, 72, ResampleMethod.NONE)
   };
// save for web options;
var exportOpts = new ExportOptionsSaveForWeb();
exportOpts.quality = 100;
exportOpts.format = SaveDocumentType.JPEG;
exportOpts.includeProfile = true;
// array of dimensions;
var theDimensions = [[1242,1656,"_mainz"], [414, 552, "_main"], [165, 220, "_cat"], [66, 88, "_mainthumb"]];
// set history state to return to;
var histState = theCopy.activeHistoryState;
// work through array;;
for (var m = 0; m < theDimensions.length; m++) {
// resize, set to bicubic if bicubicsharper is not ideal;
   theCopy.resizeImage (theDimensions[m][0], theDimensions[m][1], 72, ResampleMethod.BICUBICSHARPER);
// export;
   theCopy.exportDocument (new File(documentPath+"/"+basename+theDimensions[m][2]+".jpg"), ExportType.SAVEFORWEB, exportOpts);
// return;
   theCopy.activeHistoryState = histState;
   };
// close document;
theCopy.close(SaveOptions.DONOTSAVECHANGES);
}
else {alert ("please open a document of 1242px x 1656px")};
// reset;
preferences.rulerUnits = originalRulerUnits;
pattesdours

RGB / Multiple Image Sizes / Naming Conventions

Post by pattesdours »

Try this in ExtendScript Toolkit. All files should be in the same directory (supports PSDs, PNGs, JPGs...) If it works like you need it to, just turn off the debug variable (false instead of true) so you can use it in an action without ESTK.

RANT: ESTK CS5 is sooo slow!!

Code: Select all// multiple save for web

#target photoshop

// main variables
var debug = true;

// this is where the original images are stored
var sourcePath = "~/Desktop/IMAGES";

/* DIRECTORY STRUCTURE EXAMPLE:
~/Desktop/IMAGES/export
~/Desktop/IMAGES/parrot_alt1.jpg
~/Desktop/IMAGES/parrot_alt2.JPG
~/Desktop/IMAGES/parrot_alt3.JPG
~/Desktop/IMAGES/parrot_alt4.JPG
~/Desktop/IMAGES/parrot.JPG
*/

// this is where images will be exported
var exportPath = sourcePath + "/export";

// this array contains the files that will be processed
var imgFilesArray = buildFilesArray( new Folder(sourcePath) );

//project specs
var mainSuffixes = ["_mainz", "_main", "_cat", "_mainthumb" ];
var mainSizes = [ [1242, 1656], [414, 552], [165, 220], [66, 88] ];
var detailSuffixes = ["z", "", "thumb"];
var detailSizes = [ [1242, 1656], [414, 552], [66, 88] ];
var _resampling = ResampleMethod.BICUBIC;
var _jpgQuality = 80;

// CALL TO MAIN FUNCTION

main();

// FUNCTIONS

// main stuff
function main()
{
   // prepare Photoshop for  export
   app.preferences.rulerUnits = Units.PIXELS;

   // loop through "files" array
   for(var i = 0; i < imgFilesArray.length; i++)
   {
      var mainFile = new File(imgFilesArray[0]);
      if(debug) $.writeln("Opening file:" + mainFile);
      var doc = app.open( mainFile ); //app.activeDocument;
      convertColorMode();
      var originalHistoryState = doc.activeHistoryState;
      
      // deal with main image
      for(var j = 0; j < mainSuffixes.length; j++)
      {
         var imgName = doc.name.match(/([^\.]+)/)[1];
         var suffix = mainSuffixes[j];
         var imgWidth = mainSizes[j][0];
         var imgHeight = mainSizes[j][1];

         // resize image if not already the right size
         if(doc.width != imgWidth || doc.height != imgHeight)
         {
            if(debug) $.writeln("Resizing image to " + imgWidth + " x " + imgHeight);
            try
            {
               doc.resizeImage(imgWidth, imgHeight, 72, _resampling);
            }
            catch(e)
            {
               if(debug) $.writeln(e);
            }
         }

         // save image
         var imgFile = new File(exportPath + "/" + imgName + suffix + ".jpg");
         exportJPEG(imgFile, _jpgQuality);

         // restore history state
         if(debug) $.writeln("Restoring history state...");
         doc.activeHistoryState = originalHistoryState;
      }

      // when done, close doc
      if(debug) $.writeln("Closing original document.");
      doc.close(SaveOptions.DONOTSAVECHANGES);
      
      // loop through detail files for same image
      for(var k = 1; k < imgFilesArray.length; k++)
      {
         var altFile = new File(imgFilesArray[k]);
         if(debug) $.writeln("Opening file:" + altFile);
         var doc = app.open( altFile ); //app.activeDocument;
         convertColorMode();
         var originalHistoryState = doc.activeHistoryState;
         
            // deal with main image
         for(var l = 0; l < detailSuffixes.length; l++)
         {
            var imgName = doc.name.match(/([^\.]+)/)[1];
            var suffix = detailSuffixes[l];
            var imgWidth = detailSizes[l][0];
            var imgHeight = detailSizes[l][1];

            // resize image if not already the right size
            if(doc.width != imgWidth || doc.height != imgHeight)
            {
               if(debug) $.writeln("Resizing image to " + imgWidth + " x " + imgHeight);
               try
               {
                  doc.resizeImage(imgWidth, imgHeight, 72, _resampling);
               }
               catch(e)
               {
                  if(debug) $.writeln(e);
               }
            }

            // check if export directory exists
            var exportPathFolder = new Folder(exportPath);
            if(!exportPathFolder.exists)
            {
               if(debug) $.writeln("Creating folder " + exportPathFolder.fsName);
               exportPathFolder.create();
            }
         
            // save image
            var imgFile = new File(exportPath + "/" + imgName + suffix + ".jpg");
            if(debug) $.writeln("Saving file " + imgFile.fsName);
            exportJPEG(imgFile, _jpgQuality);

            // restore history state
            if(debug) $.writeln("Restoring history state...");
            doc.activeHistoryState = originalHistoryState;
         }

         // when done, close doc
         if(debug) $.writeln("Closing original document.");
         doc.close(SaveOptions.DONOTSAVECHANGES);
      }
}

if(debug) $.writeln("Job done! Thanks for watching.");   
   
}
// build files array based on naming convention
function buildFilesArray(folder)
{
   var files = folder.getFiles(/.jpg|.jpeg|.jpe|.png|.psd|.tif|.tiff/i);
   var filesArr = [];
   
   if(files.length > 0)
   {
      // get main file names
      for(var i = 0; i < files.length; i++)
      {
         // push if file name does not match "_alt"
         if( files.name.match("_alt") == null )
         {
            if(debug) $.writeln("Pushing MAIN file: " + files.name);
            filesArr.push( [ files ] );
         }
      }
      
      if(debug) $.writeln("...");
      
      // then get alt file names
      for(var j = 0; j < filesArr.length; j++)
      {
         var mainName = filesArr[j][0].name.match(/([^\.]+)/)[1];
         if(debug) $.writeln("\nMAIN: " + filesArr[j][0]);
         
         for(var k = 0; k < files.length; k++)
         {
            
            // provided main file is not exactly the same as the looped file...
            if(filesArr[j][0] != files[k])
            {
               //..and looped file name matches both the main name AND "_alt"
               if(files[k].name.match("_alt") != null && files[k].name.match(mainName) != null)
               {
            
                  if(debug) $.writeln("\tPushing " + files[k]);
                  filesArr[j].push(files[k]);
               }
            }
         }

      }
   }
   return filesArr;
}

// convert color profile
function convertColorMode()
{
   var doc = app.activeDocument;
   if(doc.mode != "DocumentMode.RGB")
   {
      if(debug) $.writeln("Converting to RGB...");
      doc.changeMode(ChangeMode.RGB);
   }
   if(debug) $.writeln("Converting color profile to sRGB IEC61966-2.1");
   doc.convertProfile ("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, true);
}

// create save-for-web JPEG file
function exportJPEG(file, quality)
{
   var doc = app.activeDocument;
     var displayDialogsSettings = app.displayDialogs;
   var opts = new ExportOptionsSaveForWeb();
   
   opts.quality = quality;
   opts.includeProfile = false;
   opts.format = SaveDocumentType.JPEG;

   if(file.exists) file.remove();

   app.displayDialogs = DialogModes.NO;
   activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
   app.displayDialogs = displayDialogsSettings;
}

// EOF
"";
jdsevier

RGB / Multiple Image Sizes / Naming Conventions

Post by jdsevier »

Thank you very much for you efforts I really appreciate it.
iMatt

RGB / Multiple Image Sizes / Naming Conventions

Post by iMatt »

Hi Jeff-
Alternatively to the generous scripts offered here, you could use an action that would save all of the various sizes of images into separate folders, which would prevent naming conflicts, and then use Bridge's Batch Renaming function to simultaneously rename all of the files in those folders with the correct sub name.

Matt