5 Replies Latest reply on Apr 21, 2017 7:12 AM by JJMack

    Need jsx script to include crop rather than a resize

    dannyc_TP

      HI there

       

      I have a script that resizes images in batches and places them in folders according to their size

       

      as far as i know this section of the jsx script deals with the resizing

       

      ------------------------------------------------------------------------------------------ --

       

      for( var j = index; j < imgCount; j++ ){

        // Create the subfolder (if it doesn't already)

        var subFolder = new Folder( newFolder.toString( ) + "/" + folders[j].name );

       

        if (!subFolder.exists){ subFolder.create( ); }

        if (!subFolder.exists){

        // Warn

        alert( "Could not create folder " + subFolder.toString( ) + "; cannot proceed without." );

        }else{

        // Open the file in Photoshop

        var docRef = open(fileList[i]);

       

        if (docRef == null){

        // Warn

        alert( "Failed to (properly) open " + name + "; can't do much more with/about it." );

        }else{

        // Resize the image

        docRef.resizeImage( folders[j].width, folders[j].height );

        docRef.flatten();

        var dupe = docRef.activeLayer.duplicate();

        dupe.applyUnSharpMask(40,1,0); // UNSHARP MASK HERE

       

        // Export for web

        var outputFile = new File( subFolder.toString( ) + "/" + name_xyz.toUpperCase() + '.jpg' );

       

      ----------------------------------------------------------------------------------------

       

      Can this be adapted to use a crop script? please let me know if the whole script is needed so you can see what exactly is going on. It has to be a jsx script and not a plugin

       

      the only bit of crop code i have is this

       

      var bounds = [375, 0, 2625, 3000];

      docRef.crop(bounds);

      docRef.flatten();

      var dupe = docRef.activeLayer.duplicate();

      dupe.applyUnSharpMask(40,1,0); // UNSHARP MASK HERE

       

      but I am having problems as the script does not work.

       

      any help in this is greatly appreciated

       

      Thanks

      Danny

        • 1. Re: Need jsx script to include crop rather than a resize
          Tomas Sinkunas Adobe Community Professional

          This seems to work just fine for me.

           

          for (var j = index; j < imgCount; j++) {
              // Create the subfolder (if it doesn't already)
              var subFolder = new Folder(newFolder.toString() + "/" + folders[j].name);
          
          
              if (!subFolder.exists) {
                  subFolder.create();
              }
              if (!subFolder.exists) {
                  // Warn
                  alert("Could not create folder " + subFolder.toString() + "; cannot proceed without.");
              } else {
                  // Open the file in Photoshop
                  var docRef = open(fileList[i]);
          
          
                  if (docRef == null) {
                      // Warn
                      alert("Failed to (properly) open " + name + "; can't do much more with/about it.");
                  } else {
                      // Resize the image
                      // docRef.resizeImage(folders[j].width, folders[j].height);
          
          
                      // Crop document
                      var bounds = [375, 0, 2625, 3000];
                      docRef.crop(bounds);
          
          
                      docRef.flatten();
                      var dupe = docRef.activeLayer.duplicate();
                      dupe.applyUnSharpMask(40, 1, 0); // UNSHARP MASK HERE
          
          
          
          
                      docRef.flatten();
                      var dupe = docRef.activeLayer.duplicate();
                      dupe.applyUnSharpMask(40, 1, 0); // UNSHARP MASK HERE
          
          
                      // Export for web
                      var outputFile = new File(subFolder.toString() + "/" + name_xyz.toUpperCase() + '.jpg');
                  }
              }
          }
          
          • 2. Re: Need jsx script to include crop rather than a resize
            JJMack Most Valuable Participant

            dannyc_TP  wrote

             

            HI there

             

            I have a script that resizes images in batches and places them in folders according to their size

            Cropping an image deletes part of the image.  The Image composition is changed.  There are a countless number of way an image can be cropped.

             

            You seem to be interested in cropping a set on images that are all the same size to a center crop of a particular size.   That is easily to do with a simple batch Action.

             

            However if you have different size images you need to have some rules for what you want for output image file.  Like you want centered crops of a particular Aspect Ratio that can be any size or resized to a particular size image.

             

            Adobe Photoshop Ships with a Script. Photoshop menu File>Scripts> Image Processor that you can uses to batch Process your Image Files.  There is a better more powerful Image Processor Pro plug-in script that you can download and install into Photoshop and Bridge.  In Photoshop menu File>Automate>Image Processor Pro...

             

            Using these scripts and simple actions you should be able batch Process your images the way you want.

             

            If you look at my Crafting Actions Package you will see I have included more that a dozen scripts that you can use in actions to help you do things you can not normally do in actions alone.   One of the Plug-in Script I wrote and include is Aspect Ratio Selection.   With that Plug-in you can record an action to center crop images to a particular Aspect Ratio.

             

            IMO you should look into the Scripts Image Processor and Image Processor Pro and my crafting action package.  You are making this job harder then it is.  Forget about the old script that you have..... it is not programmed to do what you want to do.    The Image Processor Scripts are.

             

            Crafting Actions Package UPDATED Aug 10, 2014 Added Conditional Action steps to Action Palette Tips.
            Contains

            Example
            Download

            • 3. Re: Need jsx script to include crop rather than a resize
              dannyc_TP Level 1

              Thank you very much. I will be trying this out and will get back to you. much appreciated

              • 4. Re: Need jsx script to include crop rather than a resize
                dannyc_TP Level 1

                Hi there

                 

                This is the entire script

                 

                ------------------------------------------------------------------------------------------ ---------------------------------------------------------------------------

                 

                // MODIFICATION NOTES:

                // - - - - - - - - - -

                // Modified on 25 February by K van Reenen - changed addtional image size from 170 x 170px to 230 x 230px

                 

                 

                // This script prompts the user for an input directory, which contains JPEG source images.

                // The images are loaded, their sizes transformed, and copies of the new images stored in a new directory structure.

                 

                 

                // Constants

                var FILE_TYPE = ".tif"; // The type of files that this script works on -- you can change

                var SEARCH_MASK = "*" + FILE_TYPE; // Image file filter to find only those files

                 

                 

                // Save current dialog preferences

                var startDisplayDialogs = app.displayDialogs;

                var startRulerUnits = app.preferences.rulerUnits;

                 

                 

                // Don't display dialogs, and set the units used to be pixels

                app.displayDialogs = DialogModes.NO;

                app.preferences.rulerUnits = Units.PIXELS;

                 

                 

                // Setup the array of folder names

                var folders = [

                  { 'name' : 'cropped',

                  'width' : 2250,

                  'height' : 3000

                  }

                ];

                 

                 

                /*

                function compare(a,b) {

                  if (a.height*a.width < b.height*b.width)

                     return 1;

                  if (a.height*a.width > b.height*b.width)

                    return -1;

                  return 0;

                }

                 

                 

                folders.sort(compare);

                 

                 

                */

                 

                 

                 

                 

                try {

                  // Ask user for input folder

                  var inputFolder = Folder.selectDialog("Select a folder to process");

                  if (inputFolder == null)

                  throw X_NOINPUT;

                 

                  // Make the selected folder current

                  Folder.current = inputFolder;

                 

                    // get all files in the input folder

                  var fileList = inputFolder.getFiles(SEARCH_MASK);

                 

                 

                  // Open each file in turn

                  for( var i = 0; i < fileList.length; i++ ){

                     // Only want to open non-hidden files (and no folders)

                  if ((fileList[i] instanceof File) && (fileList[i].hidden == false)){

                  // Get the file's name

                  var name_xyz = fileList[i].name.toLowerCase( );

                  // Check if the name is suffixed with "_v1" or "_v2" -> some images received early on were, but we want to remove it

                  if (name_xyz.indexOf( "_v1" ) > -1){

                  name_xyz = name_xyz.substring( 0, name_xyz.indexOf( "_v1" ) );

                  }else if (name_xyz.indexOf( "_v2" ) > -1){

                  name_xyz = name_xyz.substring( 0, name_xyz.indexOf( "_v2" ) );

                  }else{

                  name_xyz = name_xyz.substring( 0, name_xyz.indexOf( FILE_TYPE ) );

                  }

                 

                  // All file names should specify a suffix, which we want to further strip away to generate the top-level folder name

                  var folder_name = ""

                  if(name_xyz.indexOf('_') >= 0) folder_name = name_xyz.toUpperCase().substring( 0, name_xyz.indexOf( "_" ) );

                  else folder_name = name_xyz.toUpperCase();

                 

                  // Create a folder to contain the outputs

                  var newFolder = new Folder( folder_name );

                 

                  // If the folder doesn't exist, create it

                  if (!newFolder.exists){ newFolder.create( ); }

                  if (!newFolder.exists){

                  // Warn

                  alert( "Could not create folder " + newFolder.toString( ) + "; cannot proceed without." );

                  }else{

                  // Loop through the sizes

                  var index = 0, imgCount = folders.length - 1;

                  if(name_xyz.substring(11,13) == "_9"){

                  index = 0;

                  imgCount = 1;

                  name_xyz = name_xyz.replace('_9','')

                  }

                 

                 

                  for( var j = index; j < imgCount; j++ ){

                  // Create the subfolder (if it doesn't already)

                  var subFolder = new Folder( newFolder.toString( ) + "/" + folders[j].name );

                 

                 

                  if (!subFolder.exists){ subFolder.create( ); }

                  if (!subFolder.exists){

                  // Warn

                  alert( "Could not create folder " + subFolder.toString( ) + "; cannot proceed without." );

                  }else{

                  // Open the file in Photoshop

                  var docRef = open(fileList[i]);

                 

                 

                  if (docRef == null){

                  // Warn

                  alert( "Failed to (properly) open " + name + "; can't do much more with/about it." );

                  }else{

                  // Crop the image

                  var bounds = [375, 0, 2625, 3000];

                  docRef.crop(bounds);

                  docRef.flatten();

                  var dupe = docRef.activeLayer.duplicate();

                  dupe.applyUnSharpMask(40,1,0); // UNSHARP MASK HERE

                 

                  // Export for web

                  var outputFile = new File( subFolder.toString( ) + "/" + name_xyz.toUpperCase() + '.jpg' );

                 

                 

                  if(folders[j].name == "cropped"){

                  if(name_xyz.indexOf('_') == -1){

                  if (docRef.bitsPerChannel != BitsPerChannelType.EIGHT) docRef.bitsPerChannel = BitsPerChannelType.EIGHT;

                  var jpgSaveOptions = new JPEGSaveOptions();

                  jpgSaveOptions.embedColorProfile = true;

                  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

                  jpgSaveOptions.matte = MatteType.NONE;

                  jpgSaveOptions.quality = 12;

                  docRef.saveAs(outputFile, jpgSaveOptions, true,Extension.UPPERCASE);

                  }

                  } else{

                  var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb( );

                 

                 

                  exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;

                  exportOptionsSaveForWeb.optimized = false;

                  exportOptionsSaveForWeb.quality = 60;

                  exportOptionsSaveForWeb.interlaced = true;

                  exportOptionsSaveForWeb.includeProfile = false;

                  exportOptionsSaveForWeb.blur = 0;

                  docRef.exportDocument( outputFile, ExportType.SAVEFORWEB, exportOptionsSaveForWeb );

                  }

                  dupe.remove();

                  }

                  }

                  }

                  // Close the Photoshop file

                  docRef.close(SaveOptions.DONOTSAVECHANGES);

                  }

                  }

                    }

                }

                 

                 

                catch (exception) {

                 

                 

                  // Show degbug message and then bail out

                  alert(exception);

                }

                 

                 

                finally {

                 

                 

                  // Restore application preferences

                  app.displayDialogs = startDisplayDialogs;

                  app.preferences.rulerUnits = startRulerUnits;

                }

                 

                ------------------------------------------------------------------------------------------ ----------------------------------------------------

                 

                When i run this script i get an Undefined error. The script stops and nothing is created. Can you see where i am going wrong here?

                 

                many thanks once again

                • 5. Re: Need jsx script to include crop rather than a resize
                  JJMack Most Valuable Participant

                  You need to learn something about scripting Photoshop or stop trying to modify Photoshop scripts code. What line in your script uses an undefined variable.  Fix that error you created.

                   

                  If you want to hack Photoshop scripts you need to be able to debug and fix your hack.