3 Replies Latest reply on Nov 29, 2014 7:59 AM by KingStumpy

    Script That Crops Then Saves and Repeats?




      I am currently new to the scripting on Photoshop. Recently I have started to perform some very repetitive actions and was looking for alternatives to get them done. I looked around the internet for a certain script, but cannot find it. The scripts function would be the following...


      • Have a set pixel ratio (2048x2048)
      • Start in one of the corners
      • Crop the image (Mine is currently a little more than 20,000x20,000 pixels) down to the pixel ratio
      • Save that portion of the cropped image in png
      • Move left or right or down (whichever is logically next)
      • Repeat the process until the whole file has been saved in smaller chunks


      If anyone knows if this script exists please point me to the correct resource, this would help me so, so, so much.


      For anyone who is curious, here is the reason why I need a script that does this.

      Currently I am working on the starting stages of a very large platformer game. Currently art seems to be the most heavy task on the game. The platforming world is going to be very large and hopeful everything will be unique (as in no repeating patterns or textures). There will be two artist working on the game, myself included. I was trying to find a more efficient way of saving these very large scale files in chunks. My current file or test file is around 20,000x20,000 pixels. Doing the math to save one layer of this image in 2048 block images will take 100 repetitive save motions. Take this number times 3 for the amount of layers I need saved and it jumps up to 300. In the end the game will be somewhere around 20,000x100,000 pixels, which will just kill me if I have to do it by hand. It would take days.


      I have tried using slicing and the save for web option, but Photoshop cannot handle the large file. If there are any other methods that I can use to save a Photoshop image in 2048x2048 chunks easily, please let me know. This is a sort of make it or break it deal for the games art on whether we can make it more unique or have to use a lot of repeating patterns.

        • 1. Re: Script That Crops Then Saves and Repeats?
          c.pfaffenbichler Level 9

          20.000 is not the product of a multiplication of 2048 and an integer.


          I can’t remember having come across any Script that fits your needs perfectly.

          If no one else can point to one and as the task is a somewhat specific one I wonder if you should try hiring someone to do it for you (if you should not be able to accomplish it yourself).

          ps-scripts.com • Index page



          Does this help? (You’d need to amend the line

          if (app.documents.length > 0) {main(300, 400)}; 


          // make crops of an image according to a specific size;
          // 2014, use at your own risk;
          #target "photoshop-70.032"
          if (app.documents.length > 0) {main(300, 400)};
          ////// function //////
          function main (width, height) {
          // set to pixels and 72ppi;
          var myDocument = app.activeDocument;
          var docName = myDocument.name;
          var basename = docName.match(/(.*)\.[^\.]+$/)[1];
          var docPath = myDocument.path;
          // change to 72ppi;
          var originalRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          var originalResolution = myDocument.resolution;
          myDocument.resizeImage (null, null, 72, ResampleMethod.NONE);
          var theState = myDocument.activeHistoryState;
          var theCounter = 0;
          var theVert0 = 0;
          for (var n = 0; n < Math.ceil (myDocument.height / height); n++) {
          theVert0 = height * n;
          var theVert1 = theVert0 + height;
          var theHor0 = 0;
          for (var m = 0; m < Math.ceil (myDocument.width / width); m++) {
          theHor0 = width * m;
          var theHor1 = theHor0 + width;
          // crop and save copy;
          cropThis(theVert0, theHor0, theVert1, theHor1);
          saveCopyAsTif (myDocument, docPath+"/"+docName+"_"+bufferNumberWithZeros (theCounter,3)+"_hor"+bufferNumberWithZeros (m, 3)+"_ver"+bufferNumberWithZeros (n, 3)+".tif");
          myDocument.activeHistoryState = theState;
          // reset;
          app.preferences.rulerUnits = originalRulerUnits;
          myDocument.resizeImage (null, null, originalResolution, ResampleMethod.NONE);
          ////// crop //////
          function cropThis (x1, x2, x3, x4) {
          // =======================================================
          var idCrop = charIDToTypeID( "Crop" );
              var desc7 = new ActionDescriptor();
              var idT = charIDToTypeID( "T   " );
                  var desc8 = new ActionDescriptor();
                  var idTop = charIDToTypeID( "Top " );
                  var idRlt = charIDToTypeID( "#Rlt" );
                  desc8.putUnitDouble( idTop, idRlt, x1 );
                  var idLeft = charIDToTypeID( "Left" );
                  var idRlt = charIDToTypeID( "#Rlt" );
                  desc8.putUnitDouble( idLeft, idRlt, x2 );
                  var idBtom = charIDToTypeID( "Btom" );
                  var idRlt = charIDToTypeID( "#Rlt" );
                  desc8.putUnitDouble( idBtom, idRlt, x3 );
                  var idRght = charIDToTypeID( "Rght" );
                  var idRlt = charIDToTypeID( "#Rlt" );
                  desc8.putUnitDouble( idRght, idRlt, x4 );
              var idRctn = charIDToTypeID( "Rctn" );
              desc7.putObject( idT, idRctn, desc8 );
              var idAngl = charIDToTypeID( "Angl" );
              var idAng = charIDToTypeID( "#Ang" );
              desc7.putUnitDouble( idAngl, idAng, 0.000000 );
              var idDlt = charIDToTypeID( "Dlt " );
              desc7.putBoolean( idDlt, false );
              var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );
              var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );
              var idtargetSize = stringIDToTypeID( "targetSize" );
              desc7.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize );
          executeAction( idCrop, desc7, DialogModes.NO );
          ////// save pdf //////
          function saveCopyAsTif (myDocument, thePath) {
          // tif options;
          tifOpts = new TiffSaveOptions();
          tifOpts.embedColorProfile = true;
          tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
          tifOpts.alphaChannels = false;
          tifOpts.byteOrder = ByteOrder.MACOS;
          tifOpts.layers = false;
          // save copy;
          myDocument.saveAs((new File(thePath)), tifOpts, true);
          ////// buffer number with zeros //////
          function bufferNumberWithZeros (number, places) {
            var theNumberString = String(number);
            for (var o = 0; o < (places - String(number).length); o++) {
            theNumberString = String("0" + theNumberString)
            return theNumberString
          • 2. Re: Script That Crops Then Saves and Repeats?
            c.pfaffenbichler Level 9

            Have you been able to test the Script?

            • 3. Re: Script That Crops Then Saves and Repeats?
              KingStumpy Level 1

              Yes, this script does for the most part what I want sorry for the long reply. I've been with my family for Thanksgiving time. I messed around with it and got it to work ! Thank you very much!


              - Raymond Stump